How to redirect to previous page in Django after POST request

前端 未结 4 1842
自闭症患者
自闭症患者 2020-12-07 19:03

I face a problem which I can\'t find a solution for. I have a button in navbar which is available on all pages and it is a button responsible for creating some content.

4条回答
  •  情书的邮戳
    2020-12-07 19:23

    You can add a next field to your form, and set it to request.path. After you processed your form you can redirect to the value of this path.

    template.html

    {% csrf_token %} {{ form }}

    views.py

    next = request.POST.get('next', '/')
    return HttpResponseRedirect(next)
    

    This is roughly what django.contrib.auth does for the login form if I remember well.

    If you pass through an intermediate page, you can pass the 'next' value via the querystring:

    some_page.html

    Go to my form!
    

    template.html

    {% csrf_token %} {{ form }}

提交回复
热议问题