Python 带DeleteView的引导盒可以';在Django中找不到url

Python 带DeleteView的引导盒可以';在Django中找不到url,python,django,bootbox,Python,Django,Bootbox,我正在使用Bootbox删除应用程序中的列表,但url不起作用 以下是脚本: <script> $(".delete-file").click(function () { var button = $(this); let id = button.attr("data-id"); bootbox.confirm("Are you sure you want

我正在使用Bootbox删除应用程序中的列表,但url不起作用

以下是脚本:

<script>
    $(".delete-file").click(function () {
            var button = $(this);
            let id = button.attr("data-id");
            bootbox.confirm("Are you sure you want to delete this file?",
                function (result) {
                    if (result) {
                        $.ajax({
                            method: "GET",
                            url: "{% url 'delete_file' pk=id %}",
                            success: function(){
                            #success
                         }
                      });
                    }
                });
        });
</script>
模板:

{% for file in files %}
    <div class="col-md-6">
        <div class="cards">
            <div class="row no-gutters border rounded  flex-md-row mb-4 shadow-sm h-md-250">
                <div class="col p-4 d-flex flex-column position-static">
                    <h3 class="my-1"><a href="{{ file.file.url }}">{{file.name}}</a></h3>
                    <div style="display:flex">
                        {% for tag in file.tags.all %}
                        <a href="{% url 'tagged' tag.slug %}" class="mr-1 badge badge-info">#{{ tag }}</a>
                        {% endfor %}
                    </div>
                    <p class="mb-auto">{{file.name}}</p>
                    <p class="mb-auto text-muted">{{file.time_uploaded}}</p>
                        <form action="{% url 'file_newdescription' pk=file.pk %}" method="post">
                        {% csrf_token %}
                         <input type="text" class="form-control" value="{{ file.additional_description }}" name="additional_description">
                        <input type="submit" id="set" class="pull-right"/>
                        </form>
                    <p class="mb-auto">{{file.additional_description}}</p>
                    <a href="{{ file.file.url }}" download>Download</a>
                     <a data-id="{{ file.pk }}" class="btn-link delete-file">Delete</a>
                </div>
            </div>
        </div>
    </div>
{% endfor %}
{%用于文件中的文件%}
{%for file.tags.all%中的标记}
{%endfor%}

{{file.name}

{{file.time\u upload}

{%csrf_令牌%}

{{file.additional_description}

删除 {%endfor%}
我得到的错误是:

找不到具有关键字参数“{pk':''}”的“delete_file”的反向。已尝试1个模式:[“删除/(?P\d+/$”]


您的django版本是什么版本该版本是3.1。对url模式使用路径函数
path('delete/',views.FileDeleteView.as_view(),name='delete_file'),
,根据您的错误,I'd为空。您缺少对象的ID。发布视图和模板在您的视图请求上下文中也是“文件”?但我已将ID分配给数据ID,并在脚本中将其放入名为ID的变量中。数据ID属性正在工作,并且具有正确的ID,但仍不工作。
<a data-id="{{ file.pk }}" class="delete-file">Delete</a>
def index(request):
if request.method == 'POST':
    form = FileUploadForm(request.POST, request.FILES)
    if form.is_valid():
        form.instance.user = request.user
        form.save()
else:
    form = FileUploadForm
allTags = Tag.objects.all()
Tag.objects.annotate(ntag=Count('taggit_taggeditem_items')).filter(ntag=0).delete()
user = request.user
files = Uploaded.objects.filter(user=user)
context = {'form': form, 'allTags': allTags, 'files': files}
return render(request, 'index.html', context)
{% for file in files %}
    <div class="col-md-6">
        <div class="cards">
            <div class="row no-gutters border rounded  flex-md-row mb-4 shadow-sm h-md-250">
                <div class="col p-4 d-flex flex-column position-static">
                    <h3 class="my-1"><a href="{{ file.file.url }}">{{file.name}}</a></h3>
                    <div style="display:flex">
                        {% for tag in file.tags.all %}
                        <a href="{% url 'tagged' tag.slug %}" class="mr-1 badge badge-info">#{{ tag }}</a>
                        {% endfor %}
                    </div>
                    <p class="mb-auto">{{file.name}}</p>
                    <p class="mb-auto text-muted">{{file.time_uploaded}}</p>
                        <form action="{% url 'file_newdescription' pk=file.pk %}" method="post">
                        {% csrf_token %}
                         <input type="text" class="form-control" value="{{ file.additional_description }}" name="additional_description">
                        <input type="submit" id="set" class="pull-right"/>
                        </form>
                    <p class="mb-auto">{{file.additional_description}}</p>
                    <a href="{{ file.file.url }}" download>Download</a>
                     <a data-id="{{ file.pk }}" class="btn-link delete-file">Delete</a>
                </div>
            </div>
        </div>
    </div>
{% endfor %}