如何覆盖和扩展基本的 Django 管理模板? [英] How to override and extend basic Django admin templates?

查看:35
本文介绍了如何覆盖和扩展基本的 Django 管理模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何覆盖管理模板(例如 admin/index.html),同时扩展它(参见 https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-管理模板)?

首先 - 我知道这个问题之前已经被问过和回答过(见 Django:覆盖和扩展应用模板),但正如答案所说,如果您使用的是 app_directories 模板加载器(大多数情况下),它不能直接适用.

我目前的解决方法是制作副本并从它们扩展,而不是直接从管理模板扩展.这很好用,但它真的很混乱,并且在管理模板更改时增加了额外的工作.

它可以为模板考虑一些自定义扩展标签,但如果已经存在解决方案,我不想重新发明轮子.

附带说明:有谁知道这个问题是否会由 Django 自己解决?

解决方案

更新:

阅读适用于您的 Django 版本的文档.例如

https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#admin-overriding-templateshttps://docs.djangoproject.com/en/2.0/ref/contrib/admin/#admin-overriding-templateshttps://docs.djangoproject.com/en/3.0/ref/contrib/admin/#admin-overriding-templates

2011 年的原始答案:

大约一年半前我遇到了同样的问题,我在 djangosnippets.org 上找到了一个不错的 模板加载器 这使得这很容易.它允许您在特定应用程序中扩展模板,使您能够创建自己的 admin/index.html,从管理应用程序扩展 admin/index.html 模板.像这样:

{% extends "admin:admin/index.html";%}{% 块侧边栏 %}{{block.super}}<div><h1>额外链接</h1><a href="/admin/extra/">我的额外链接</a>

{% 结束块 %}

我在 博客文章在我的网站上.

How do I override an admin template (e.g. admin/index.html) while at the same time extending it (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template)?

First - I know that this question has been asked and answered before (see Django: Overriding AND extending an app template) but as the answer says it isn't directly applicable if you're using the app_directories template loader (which is most of the time).

My current workaround is to make copies and extend from them instead of extending directly from the admin templates. This works great but it's really confusing and adds extra work when the admin templates change.

It could think of some custom extend-tag for the templates but I don't want to reinvent the wheel if there already exists a solution.

On a side note: Does anybody know if this problem will be addressed by Django itself?

解决方案

Update:

Read the Docs for your version of Django. e.g.

https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#admin-overriding-templates https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#admin-overriding-templates https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#admin-overriding-templates

Original answer from 2011:

I had the same issue about a year and a half ago and I found a nice template loader on djangosnippets.org that makes this easy. It allows you to extend a template in a specific app, giving you the ability to create your own admin/index.html that extends the admin/index.html template from the admin app. Like this:

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

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

I've given a full example on how to use this template loader in a blog post on my website.

这篇关于如何覆盖和扩展基本的 Django 管理模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆