Home  >  Article  >  Backend Development  >  How to Override and Extend Django Admin Templates with App-Specific Extensions?

How to Override and Extend Django Admin Templates with App-Specific Extensions?

DDD
DDDOriginal
2024-11-12 02:27:02139browse

How to Override and Extend Django Admin Templates with App-Specific Extensions?

Overriding and Extending Django Admin Templates with App-Specific Extensions

Overriding Django admin templates while extending them can be a challenge when using the app_directories template loader. To address this, consider utilizing a custom template loader that supports extending templates from specific apps.

Solution: Custom Template Loader

A custom template loader such as SmartTemplates allows you to specify the template to extend within a specific app. This enables you to create your own admin/index.html template that inherits from the admin/index.html template in the admin app.

For example:

{% extends "admin:admin/index.html" %}

{% block sidebar %}
    {{ block.super }}
    <div>
        <h1>Extra Links</h1>
        <a href="/admin/extra/">My Extra Link</a>
    </div>
{% endblock %}

Usage:

  1. Install the SmartTemplates package.
  2. Configure the TEMPLATE_LOADERS setting to include 'smart_templates.loader.SmartLoader'.
  3. Place your custom admin/index.html template in your project's templates directory.

Additional Notes:

  • Django versions 2.2 and later provide built-in support for template inheritance using the template from directive, reducing the need for custom template loaders.
  • Refer to the Django documentation for your specific version for more details on template overriding and extension.

The above is the detailed content of How to Override and Extend Django Admin Templates with App-Specific Extensions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn