Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit Django admin logout template?

I want to make a very small change to the Django admin logout page.

I know how to use templates to override the Django admin templates, so I have tried to do the same thing with the logout file.

I have set up a new template at templates/registration/logged_out.html. The content of this file is as follows:

{% extends "registration/logged_out.html" %}
{% block content %}
<p>Thanks for using the site.</p>
<p><a href="../">Log in again</a></p>
<p><a href="/">Return to the home page</a></p>
{% endblock %}

However, something is definitely wrong, because when I try to log out of admin, the site stops running.

I've found the Django docs page recommending the use of AdminSite for changes to the base template and logout pages, but is this really necessary for such a tiny change?

If so, does anyone have an example of how I might set up the logout template? I'm rather intimidated by the instructions for AdminSite.

Thanks.

like image 521
Richard Avatar asked Jul 04 '11 14:07

Richard


People also ask

How do I change the admin template in Django?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

Can we customize Django admin template?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want. In this tutorial, you learned how to: Register your object models with the Django admin.

How to implement logout in Django?

Here to handle the logout functionality, we need to import the "logout" built-in Django function and "login_required" decorator. We need this decorator to make sure that users who are trying to log out are authenticated or, in simple terms - logged in.


2 Answers

The reason of manage.py runserver termination is an inheritance loop.

Django loads "registration/logged_out.html" and that it tries to load it's parent: "registration/logged_out.html". Unfortunately parent is the same template and so we end up on the template inheritance loop. Manage.py will terminate with some variant of stack overflow error...

You can easily escape the issue by extending the parent of original "registration/logged_out.html" -> "admin/base_site.html". I.e:

{% extends "admin/base_site.html" %}
{% load i18n %}

{% block breadcrumbs %}<div class="breadcrumbs"><a href="../">{% trans 'Home' %}</a></div>{% endblock %}

{% block content %}
<p>Thanks for using the site.</p>
<p><a href="../">Log in again</a></p>
<p><a href="/">Return to the home page</a></p>
{% endblock %}
like image 123
Munhitsu Avatar answered Sep 28 '22 12:09

Munhitsu


You're getting a template import loop. The template loader won't load the base template form wherever you've got Django installed, because it sees that you have that template in your project's template folder.

I think you'll need to copy the log out template from where you have Django installed to your project's template folder. Unfortunately that's the only way that seems to work. This method also means that if updates are made to the Django admin templates, you'll have to manually apply them to your modified templates.

like image 40
Alex Jillard Avatar answered Sep 28 '22 11:09

Alex Jillard