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

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

问题描述

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

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)?

首先 - 我知道这个问题已经被询问并回答(见 Django:覆盖并扩展应用程序模板),但是答案说如果你是使用app_directories模板加载器(这是大部分时间)。

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.

在旁注:有人知道Django本身是否会解决这个问题?

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

推荐答案

我有一年半以前有同样的问题,我发现一个很好的 djangosnippets.org上的模板加载器,这使得这很容易。它允许您在特定应用程序中扩展模板,使您能够创建自己的 admin / index.html ,从admin应用程序扩展admin / index.html模板。像这样:

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天全站免登陆