Django 3.0.6。
pcask / urls.py
urlpatterns = [
path('image/', include(('image.urls', 'image'), namespace="image")),
]
/pcask/image/urls.py
urlpatterns = [
path('bulk_upload/<int:pk>/', SeleniumBulkImageUpload.as_view(), name='bulk_upload'),
]
views.py
class SeleniumBulkImageUpload(FormView):
form_class = FileFieldForm
template_name = "image/selenium_bulk_upload.html"
success_url = "/"
def post(self, request, *args, **kwargs):
pk = kwargs.get("pk") # Breakpoint
form_class = self.get_form_class()
form = self.get_form(form_class)
files_dir = form.data['file_field']
_upload_files(pk=pk, files_dir=files_dir)
return super().post(request, *args, **kwargs)
image / selenium_bulk_upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="{% url 'image:bulk_upload' 1 %}" enctype="multipart/form-data">
{% csrf_token %} {{ form }}
<input type="submit">
</form>
</body>
</html>
到目前为止,第一张图片的动作路径已硬编码。这是用于调试。 无论如何,根本不应该采取任何行动。
URLS
路由似乎还可以。
在终端中:
>>> reverse("image:bulk_upload", kwargs={'pk':1})
'/image/bulk_upload/1/'
在模板中:
<form method="post" action="/image/bulk_upload/1/" enctype="multipart/form-data">
<input type="hidden" name="csrfmiddlewaretoken" value="TUZ1eqgRDZmQsA49Lw0E7aY6obr6B6tjx2PTQtE9DxsaqVLOXt7S1u0UmvTd02DW"> <tr><th><label for="id_file_field">File field:</label></th><td><input type="text" name="file_field" maxlength="300" required id="id_file_field"></td></tr>
<input type="submit">
</form>
问题
获取请求被路由到SeleniumBulkImageUpload。 必要的形式显示在浏览器中。
然后我可以在IDE的控制台中看到:
[21/May/2020 18:12:42] "GET /image/bulk_upload/1/ HTTP/1.1" 200 9911
然后我可以在父类的get方法的断点处停止。
但是对于post方法,我得到:
Method Not Allowed (POST): /image/bulk_upload/1/
Method Not Allowed: /image/bulk_upload/1/
并且解释器不会在断点处停止(如代码所示)。即使我完全删除了重新定义的post方法,问题也不会消失。
我试图打开ProcessFormView(一个父类)并在其中放置断点,然后更改模板:
<form method="get" action="{% url 'image:bulk_upload' 1 %}" enctype="multipart/form-data">
结果:
嗯,它停止了。这样路由就可以了。
请求:
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8000/image/bulk_upload/1/
Content-Type: multipart/form-data; boundary=---------------------------286629394610736077032745264977
Content-Length: 409
Origin: http://localhost:8000
Connection: keep-alive
Cookie: csrftoken=Wb2wYtu6FM8z4dpvC9somiYXOkHV3qNxyAfXeDebSzG623FfTO6j8sqBRXRiM4DD; djdt=hide
Upgrade-Insecure-Requests: 1
响应:
HTTP/1.1 405 Method Not Allowed
Date: Thu, 21 May 2020 15:21:44 GMT
Server: WSGIServer/0.2 CPython/3.8.0
Content-Type: text/html; charset=utf-8
Allow: GET, PUT, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 0
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
您能在这里给我一些建议吗?
答案 0 :(得分:0)
您无需在super().post()
方法中调用post()
;相反,您可以直接返回HttpResponse
(或使用redirect()
)。
示例,而不是一行:
return super().post(request, *args, **kwargs)
尝试这样的事情
return redirect(self.success_url)
或
return HttpResponseRedirect(self.get_success_url())
对您有用吗?