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

DDD
Release: 2024-11-12 02:27:02
Original
176 people have browsed it

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 %}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template