django logout redirects me to administration page

前端 未结 11 2073
生来不讨喜
生来不讨喜 2020-12-14 14:32

I have provided a simple login functionality. For logout, I tried to use the built-in one. This is my urls.py:

(r\'\', include(\'django.contrib.auth.urls\'))         


        
相关标签:
11条回答
  • 2020-12-14 15:27

    If you are seeing the log out page of the Django administration site instead of your own log out page (your_application/templates/registration/logged_out.html), check the INSTALLED_APPS setting of your project and make sure that django.contrib.admin comes after 'your_application'. Both templates are located in the same relative path and the Django template loader will use the first one it finds.

    0 讨论(0)
  • 2020-12-14 15:28

    I'm surprised no one has mentioned this, you can put this in your settings.py to redirect when logging in and logging out:

    LOGIN_REDIRECT_URL = '/go-here-after-login/'
    LOGOUT_REDIRECT_URL = '/go-here-after-logout/'
    
    0 讨论(0)
  • 2020-12-14 15:32

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

    (r'^logout/$', 'django.contrib.auth.views.logout',
        {'next_page': '/logged_out/'})
    
    0 讨论(0)
  • 2020-12-14 15:34

    This is all fairly well explained in the manual, is there anything specific you don't understand?

    https://docs.djangoproject.com/en/dev/topics/auth/default/#how-to-log-a-user-out

    from django.contrib.auth import logout
    
    def logout_view(request):
        logout(request)
        # Redirect to a success page.
    

    Alternatively if you don't want to create your own view

    https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views.logout

    {% url 'logout' next='/some/url/' %}
    
    0 讨论(0)
  • 2020-12-14 15:35

    Go to settings.py and add this code. "/" will redirect you to home

    # Where to redirect during authentication
    LOGIN_REDIRECT_URL = "/" #To go to home after login instead of getting redirected to accounts/profile on login which is default
    LOGOUT_REDIRECT_URL = "/" #To logout back to the home page instead of the default admin logout page
    
    0 讨论(0)
提交回复
热议问题