How to Override Admin Template in Django: Simple Steps
To override an admin template in Django, create a folder named
admin inside your app's templates directory and place your custom template with the same name as the original. Django will use your template instead of the default one when rendering the admin interface.Syntax
Override admin templates by creating a folder structure like your_app/templates/admin/ and placing the template file with the exact name you want to override. For example, to override the base admin template, create templates/admin/base_site.html.
Django searches templates in the order of your TEMPLATES setting, so your app's template will take priority over the default admin templates.
plaintext
your_app/
templates/
admin/
base_site.html # Your custom admin templateExample
This example shows how to override the Django admin's base_site.html template to change the site header text.
django
{% raw %}{% extends "admin/base_site.html" %}
{% block title %}My Custom Admin{% endblock %}
{% block branding %}
<h1 id="site-name"><a href="/admin/">My Custom Admin</a></h1>
{% endblock %}{% endraw %}Output
The Django admin site header changes from 'Django administration' to 'My Custom Admin' when you log in.
Common Pitfalls
- Not placing the template in the correct
templates/admin/folder inside your app. - Using the wrong template name or path; it must exactly match the original admin template you want to override.
- Forgetting to include your app's templates directory in the
TEMPLATES['DIRS']or ensuringAPP_DIRSisTruein settings. - Not restarting the Django server after adding templates, so changes don't appear.
plaintext
{% raw %}# Wrong: Placing template outside admin folder
your_app/templates/base_site.html
# Right: Place inside admin folder
your_app/templates/admin/base_site.html{% endraw %}Quick Reference
Summary tips for overriding Django admin templates:
- Put custom templates in
your_app/templates/admin/. - Use the exact same filename as the original admin template.
- Ensure
APP_DIRSisTrueinsettings.py. - Restart the server to see changes.
Key Takeaways
Place your custom admin templates in your app's templates/admin folder with the same filename as the original.
Ensure Django's template settings allow loading app templates by setting APP_DIRS to True.
Restart your Django server after adding or changing templates to see the updates.
Double-check the template path and name to avoid common mistakes.
Overriding admin templates lets you customize the admin interface look and feel easily.