相关文章推荐

我在Django里有一个表单,我用下面的代码提交。

  XHR.open("POST", "/route/to/post/view");
  XHR.send(formData);

而它的路由看起来像这样。

def my_view(request):
    if request.method == "GET":
        context = {"object": Perso.objects.all()}
        return render(request, "myview.html", context)
    elif request.method == "POST":
        print("post request is", request.POST)
        print("redirecting...")
        return redirect("/route/to/another-view")

我知道这样做是正确的,因为当我按下POST按钮的时候,我得到了以下的输出。

post request is <QueryDict: {'csrfmiddlewaretoken': ['6pdzvTNVO8d1KhS9w3IevFGD7w3bprBRQtUaO1TuXDS4RIkTP44IlFdYwJQB7czx'], 'vaccinator': ['tilley evan']}>
redirecting...
[12/Apr/2021 15:42:34] "POST /route/to/post/view HTTP/1.1" 302 0 
[12/Apr/2021 15:42:34] "GET /route/to/another-view HTTP/1.1" 200 3546
 
推荐文章