Logging out on django web app redirects to django admin logout page. What's going on?

Viewed 701

I'm following a tutorial in the Python Crash Course book on creating a simple web page using Django. I've just created an app to log a user in and out on my page, using Django's default user authentication system. When I logout, Django is not redirecting to the template I've created to show that the user is logged out. Instead it redirects to the Django admin logout page.

Here is my base urls.py file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/', include('users.urls')),
    path('', include('learning_logs.urls')),

]

Here is my 'users' app urls.py file.

from django.urls import path, include

app_name = 'users'
urlpatterns = [
    # Include default auth URLs
    path('', include('django.contrib.auth.urls')),
]

Here is my base.html template.

<p>
<a href="{% url 'learning_logs:index' %}">Learning Log</a> -
  <a href="{% url 'learning_logs:topics' %}">Topics</a>
  {% if user.is_authenticated %}
    Hello, {{ user.username }}.
    <a href="{% url 'users:logout' %}">Log out</a>
  {% else %}
    <a href="{% url 'users:login' %}">Log in</a>
  {% endif %}
</p>

Here is my logout.html template.

{% extends "learning_logs/base.html" %}

{% block content %}
  <p>You have been logged out. Thanks for stopping by!</p>
{% endblock content %}

Here are my installed apps in settings.py

INSTALLED_APPS = [
    # My Apps
    'learning_logs',
    'users',

    # Default Apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

When the page redirects to the Django Admin logout, the url shows up as http://localhost:8000/users/logout/ which is the url I want. Also, I am using my admin username and password to login to the web page.

Any help is appreciated!

edit: Django version being used is 3.2.6. Version being used in the tutorial is 2.2

3 Answers

in settings.py you can sett logout redirect url
Give this a try

LOGOUT_REDIRECT_URL = '/path_to_the_page'
LOGIN_URL = '/path_to_the_page'

in your settings you can do that easily how suppose you have function based view

def hello(request):
    return render (request, 'logout_redirect.html')
and in url
  path('hello/', views.hello, name="hello")

so in your settings do this

LOGIN_URL = 'accounts:login'
LOGIN_REDIRECT_URL = 'accounts:home'
LOGOUT_REDIRECT_URL = 'accounts:home'

and second way just pass in your views

@login_required
def user_logout(request):
    logout(request)
    return redirect('accounts:login') 

both the type is neccessary to be in your page first is for if a user logout from admin panel tell where to go and second for a custom user to tell where he have to redirect so you can change your logout redirect url different for both diffrent type of user or if you want to redirect on same url for both of the user pass the same path

and i do recommend you to read the full documentation of django for better iunderstanding https://docs.djangoproject.com/en/3.2/topics/auth/default/

Solved! It turns out the logout template MUST be named logged_out.html to work in this context. When I renamed it from logout.html the page redirected properly to the custom logout template.

Related