django logout redirects me to administration page

前端 未结 11 2072
生来不讨喜
生来不讨喜 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:11

    Summary of the most common solutions:

    Make sure that your_app comes before django.contrib.admin in your INSTALLED_APPS list in your settings.py file.

    Also make sure that your log out page is called 'logged_out.html' as pointed out in the answers above. Mine was called logout.html and didn't work.

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

    Just replace loaders here, and auth templates will be found in "your_progect_apps/templates/registration":

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
        },
    },
    

    ]

    Django v2.1

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

    The easiest solution is:

    1. Make sure your app comes before django.contrib.admin under installed apps in settings.py.

    2. Make sure your template is called logged_out.html.

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

    i was experiencing the same isssue following Django by example... found this url worked for me

    url(r'^logout/$', 'django.contrib.auth.views.logout', { 'template_name': 'account/logout.html',}, name='logout' ),
    
    0 讨论(0)
  • 2020-12-14 15:22

    You can put LOGOUT_REDIRECT_URL in your settings.py file with a url name to redirect to, e.g. LOGOUT_REDIRECT_URL = 'index'

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

    Tested on Django 1.6:

    What I do is adding this into my urls.py:

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

    And then used it:

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

    For the next argument, there you point to the right URL.

    Tested on Django 2.1

    Append to urlpatterns in urls.py:

    from django.contrib.auth import views as auth_views
    
    urlpatterns = [
        path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    ]
    

    And then use it in the template:

    <a href="{% url "logout" %}?next=/">logout</a>
    

    More info can be found here.

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