Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Overriding Templates

00:00 Overriding Django Admin Templates. The Django developers implemented the admin using the Django template mechanism. This made their job a little bit easier, but it also benefits you by allowing you to override those templates.

00:16 You can fully customize the admin by changing the templates used to render pages. You’ll find the templates used in the admin by looking inside the Django package in your virtual environment.

00:28 The exact location will depend on your system, but will largely be the same as the one onscreen, where (venv) represents your Python virtual environment and (python) is the Python version you’re using.

00:41 Here, you can see the admin templates being shown in Finder on my own system, and the main thing to notice is there’s a lot of them. The Django template engine has a defined order for loading templates.

00:54 When it loads a template, it uses the first template that matches the name. You can override admin templates by using the same directory structure and file names. The admin templates come in two directories.

01:07 admin/ is for the model object pages.

01:13 registration/ is for password changes and logging in and out. To customize the logout page, you need to override the right file. The relative path leading to the file has to be the same as the one being overridden.

01:27 The file you’re interested in is registration/logged_out.html. Start by creating the directory in the School project.

01:41 Next, Django needs to be told about the new template directory inside the School/ file. Look for the TEMPLATES directive and add the folder to the list of directories.

02:02 The template engine searches directories in the 'DIRS' option before the application directories, so anything with the same name as an admin template will be loaded instead. To see this in action, copy Django’s default logged_out.html file into your templates/registration/ directory, and then modify it as seen onscreen now.

02:30 If you click LOG OUT, then you’ll see the customized message. Django admin templates are deeply nested and maybe not that intuitive, but you do have full control over their presentation if you need it.

02:45 Some packages including Grappelli and Django Admin Bootstrap have fully replaced the Django admin templates to change their appearance. Projects like these can sometimes not yet be compatible with the latest Django version, so you may need to follow their changes closely. That said, if you want to see the power of overriding admin templates, they’re well worth checking out.

Avatar image for support27

support27 on Feb. 26, 2022

For whatever reason I can not complete “Overriding Django Admin Templates” pretty straightforward mkdir -p templates/registration in the School directory and copy over the logged_out.html and edit it - but no luck and yes also not to mention the 'DIRS': [BASE_DIR / "templates"], I tried both BASE_DIR and os.path.join -

No luck.... any insight on this - I followed all steps to the “T”

pip freeze
* asgiref==3.5.0
* Django==4.0.2
* form==0.0.1
* sqlparse==0.4.2
* tzdata==2021.5
Avatar image for Darren Jones

Darren Jones RP Team on March 8, 2022

@support27 Can you tell me more about what’s happening - how far you’ve got and the exact issue? If you can copy the code for the second issue in, I may be able to help.

Become a Member to join the conversation.