Roman Starkov
Roman Starkov

Reputation: 61502

How to modify a template of a reusable app in Django?

For example, let's say I want to modify the breadcrumbs block of the admin/change_list.html template in the Django admin. If I try to override this template like this:

{% extends "admin/change_list.html" %}
{% block breadcrumbs %} ... my changes ... {% endblock %}

then Django goes into an infinite recursion when trying to load my override, because the "extends" tag tries to load the override rather than the original template.

An obvious way would be to copy & paste the entire source template, but this is exactly the thing I'm trying to avoid. So how can I modify a template without copying and pasting all of it into my project?

Upvotes: 1

Views: 1588

Answers (1)

LeafStorm
LeafStorm

Reputation: 3127

The complete details on overriding the admin templates are here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates.

If you're only trying to change the template for a single app, you can simply save it as app_label/change_list.html. If you're trying to change it for a single model, you can save it as app_label/model_as_underscores/change_list.html.

If you're trying to change it for all your stuff, you can create a new template with a different name, and set its name as the change_list_template attribute on all your ModelAdmin subclasses. (On ModelAdmin, you can do the same thing with change_form_template, delete_confirmation_template, and object_history_template. On an AdminSite, you can override the index_template and login_template attributes in the same way.)

Both of these methods will allow you to extend from the original admin templates.

Upvotes: 3

Related Questions