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

后端 未结 11 1176
情书的邮戳
情书的邮戳 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 09:59

    If you want to set the redirection URL on client level, you can do it in the urls.py:

    (r'^management/logout/$', 'django.contrib.auth.views.logout'),
    

    And then in the template:

    <a href="{% url 'django.contrib.auth.views.logout' %}?next=/">
        Log out
    </a>
    

    Where the next, you point to the right URL.

    0 讨论(0)
  • 2020-12-02 09:59

    If you have defined your own urls (and not imported generic auth urls) and are using the standard django auth views, them you can simply add (template_name='example.html') in the path.

    path('logout/',auth_views.LogoutView.as_view(template_name='homepage.html'),name="logout")

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

    From docs you can write your own logout view (which can be just simple wrapper) overriding the 'next' page.

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

    You can redirect user anywhere by using LOGOUT_REDIRECT_URL in your setting.py file

    LOGOUT_REDIRECT_URL = 'url name to redirect'
    
    0 讨论(0)
  • 2020-12-02 10:11

    You can even use named urls for your next parameter:

    <a href="{% url 'auth_logout' %}?next={% url 'homepage' %}"> Logout</a>
    
    0 讨论(0)
  • 2020-12-02 10:12

    One easier way:

    Add 'next' parameter to your log-out request url. For example:

    <a href="{% url 'auth_logout' %}?next=/path_to_the_page"> Logout</a>
    

    Then the logout view will do the trick for you.

    For after-login-redirect, you can just simply set it in settings.py:

    LOGIN_REDIRECT_URL = '/path_to_the_page'
    LOGIN_URL = '/path_to_the_page'
    
    0 讨论(0)
提交回复
热议问题