簡體   English   中英

Django-使用表單發布到特定的URL

[英]Django - Post to specific URL using form

我想發布到特定的URL。 url具有刪除數據庫行的范圍。 URL由地址+從模型捕獲的表單中選擇的文件的pk組成。

select_file_deletion.html

{% extends "index.html" %}

{% block content %}
<!--Here the number 2 in "/App/delete/2/" needs to be replaced with the pk of the file. The logic is working. -->
    <form action="/App/delete/{{ myfile.pk }}/" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <h5>Please select one file at a time from the list below to delete it from the server.</h5>
        {% for myfile in filename %}
          <input type="checkbox" name="file_name" value="{{ myfile }}">
            <label>
                <a href="/media/{{ myfile }}">{{ myfile }}</a>
                <input type="hidden" value="{{ myfile.pk }}" name="pk">
            </label>
            <br>
        {% endfor %}
        <br>
        <button type="submit" class="btn btn-primary">Delete</button>
    </form>
{% endblock %}

項目urls.py

url(r'^delete/(?P<pk>\d+)/$', FileDeleteView.as_view(), name='APIdelete')

views.py

class SelectFileDelView(TemplateView):
    """
    This view is used to select a file from the list of files in the server.
    After the selection, it will send the file to the server.
    The server will then delete the file.
    """
    template_name = 'select_file_deletion.html'
    parser_classes = FormParser
    queryset = FileModel.objects.all()

    def get_context_data(self, **kwargs):
        """
        This function is used to render the list of files in the MEDIA_ROOT in the html template.
        """
        context = super().get_context_data(**kwargs)
        media_path = settings.MEDIA_ROOT
        myfiles = [f for f in listdir(media_path) if isfile(join(media_path, f))]
        context['filename'] = myfiles
        return context


class FileDeleteView(DeleteView):
    """
    This class contains the method to delete a file interacting directly with the API.
    DELETE requests are accepted.
    """
    # TODO: Fix, still not working
    model = FileModel
    fields = ['file']
    template_name = 'delete_success.html'
    success_url = '/delete_success/'

App / urls.py

# Url to select a file to be deleted and confirm the upload
url('filedelete/', SelectFileDelView.as_view(), name='file_delete'),
url('delete_success/', FileDeleteView.as_view(), name='delete_success')

錯誤 :請求URL行未捕獲包括pk的地址,未用所選文件的pk替換變量。

Page not found (404)
Request Method: POST
Request URL:    http://127.0.0.1:8000/App/delete//
Using the URLconf defined in DjangoRestDeepLearning.urls, Django tried these URL patterns, in this order:

^App/ ^predict/$ [name='APIpredict']
^App/ ^upload/$ [name='APIupload']
^App/ ^delete/(?P<pk>\d+)/$ [name='APIdelete']
filedelete/ [name='file_delete']
delete_success/ [name='delete_success']
The current path, App/delete//, didn't match any of these.

在打開此問題之前我檢查了一下但未解決問題:

1) 在Django中刪除帶有表單的對象

2) Django如何通過表單動作傳遞對象ID?

我假設您的模板中有myfile.pk。 您的表單操作中的網址無法正常工作,因為缺少pk。 將表單的操作替換為:

   <form action="{% url 'APIdelete' pk=myfile.pk %}" method="post" enctype="multipart/form-data">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM