How to configure where to redirect after a log out in Django?

后端 未结 11 1177
情书的邮戳
情书的邮戳 2020-12-02 09:48

Just wondering where I can set the url to redirect to after logout. I know you can set the login url. I want to redirect to my home page.

相关标签:
11条回答
  • 2020-12-02 10:14

    In your logout view, after you logout the user for good, return HttpResponseRedirect(url). Please see here for more details.

    0 讨论(0)
  • 2020-12-02 10:15

    Redirect to current page

    <a href="{% url 'logout' %}?next={{ request.path | urlencode }}">{% trans "Logout" %}</a>
    

    Tested in Django 1.9.

    See also: Is it possible to pass query parameters via Django's {% url %} template tag?

    0 讨论(0)
  • 2020-12-02 10:15

    add this in you project setting.py file LOGOUT_REDIRECT_URL = '/'

    you can write your URL between '' I use my index page for logout default redirect

    0 讨论(0)
  • 2020-12-02 10:18

    Modern Django (2017+?) has a setting called LOGOUT_REDIRECT_URL.

    Older Djangos / Original Answer

    You don't need to overwrite or wrap anything.

    According to the docs, you can just supply the next_page argument to the logout view. https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views.logout

    (r'^logout/$', 'django.contrib.auth.views.logout',
                              {'next_page': '/successfully_logged_out/'})
    
    0 讨论(0)
  • 2020-12-02 10:19

    Since Django 1.10, you can define a LOGOUT_REDIRECT_URL (see the docs)

    0 讨论(0)
提交回复
热议问题