Is there a way to override the log out
link on a Django admin page? I am trying to delete a cookie when a user logs out.
4 Answers
The best way to override home page of Django admin is to override admin template. Make your own admin template directory and create a base.html file there. I believe you are trying to override the link of log out. You need to re-write userlinks block. And I won't talk the javascript part, but you can write your base.html like below.
{% extends "admin/base.html" %}
{% load i18n %}
{% block extrahead %}
// write your js script here, may be on click of the logout link or whatever.
{% endblock %}
{% block userlinks %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
<a href="{{ docsroot }}">{% trans 'Documentation' %}</a> /
{% endif %}
<a href="{% url 'admin:password_change' %}">{% trans 'Change password' %}</a> /
<a href="{% url 'admin:logout' %}" id="yourlogoutid">{% trans 'Log out' %} </a>
{% endblock %}

- 1,496
- 14
- 23
You have two ways for doing this,
Bad way:
Go to your python packages directory and find the django-admin templates in
django/contrib/admin/templates/admin
. Here you will find thebase.html
file with the logout link(on line 44 in mine), and permanently change django admin across all projects.Good way:
You can override specific django-admin templates by putting them in the templates directory in the heirarchy like
templates/admin/[app-name]/[template-name]
. This will limit it only to the app, and is better. Find out more about the same here.

- 6,405
- 6
- 28
- 69

- 980
- 4
- 16
You can also avoid modifying template code entirely by overriding the URL path in urls.py
. Just include the override before the admin urls, like so:
# project/urls.py
from django.urls import include, path
from django.shortcuts import redirect
from django.contrib import admin
# import logout view
from app.views import logout_view
urlpatterns = [
...
path('logout/', logout_view, name='logout'),
# note the override comes before the admin URLs below
path('admin/logout/', lambda request: redirect('/logout/', permanent=False)),
path('admin/', admin.site.urls)
...
]
This example uses a redirect to the intended logout URL path, but you can also directly reference the intended logout view. I'd consider the redirect method to be more DRY.
From there, just put the cookie removal logic in your logout view:
# app/views.py
from django.shortcuts import redirect
from django.contrib.auth import logout
def logout_view(request):
logout(request)
response = redirect('/home')
response.delete_cookie('example_cookie')
return response

- 210
- 2
- 10
-
1thanks. I think this is the exact answer OP was looking for. – chip Mar 08 '22 at 12:44
Django has gone through a few version changes since the OP, but the in the current release as of this comment (Django-3.2), you can accomplish changing the logout redirect by adding: LOGOUT_REDIRECT_URL = '/your_desired_url/'
to the settings.py file in your main project
I'm pretty new to Django and to Python, but it passes my unit tests and functions as expected

- 191
- 2
- 8
-
-
Also, you can use reverse_lazy("any-url_name") with the LOGOUT_REDIRECT_URL. – Rodrigo Nogueira Jan 07 '22 at 22:53
-
LOGOUT_REDIRECT_URL tells Django which URL to serve once the user has successfully logged out. But I think the OP is asking how do you change the behavior of the logout functionality itself. – Dr Phil Jul 19 '22 at 14:12