难以覆盖 Django 管理模板

发布于 2024-10-16 02:17:52 字数 1164 浏览 2 评论 0原文

我在 Ubuntu 10.10 上使用 Django 1.2.4。我正在尝试覆盖管理模块的index.html 模板。我一直在遵循这些说明 。我也看了这个问题,但我仍然有困难。

说明说要在模板目录中创建一个 admin 目录:

templates/
    admin/
         index.html

我想覆盖 index.html 中的单个块。 (真的,我想做的就是在末尾附加一些文本。有没有比复制/粘贴整个块并更改它更简单的方法?)(更新:看起来像 {{ block.super}} 可能会有所帮助。)

为了表明我正在覆盖,我将其放在 index.html 的顶部:

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

当然,这会导致堆栈溢出(从终端):

Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in <type 'exceptions.RuntimeError'> ignored

正确的方法是什么?我尝试了每个链接问题的答案的符号链接,但这导致了以下结果:

me@mycomp:~/foo$ sudo ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/ django_admin
[sudo] password for me: 
ln: creating symbolic link `django_admin': Protocol error

我做错了什么?

I'm using Django 1.2.4 on Ubuntu 10.10. I'm trying to override the index.html template for the admin module. I've been following these instructions. I also looked at this question, but I'm still having difficulty.

The instructions say to create an admin directory in the templates directory:

templates/
    admin/
         index.html

I want to override a single block in the index.html. (Really, all I want to do is append some text to the end. Is there an easier way than copy/pasting the entire block and changing it?) (Update: Looks like {{block.super}} may help.)

To signal that I'm overriding, I put this at the top of my index.html:

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

Of course, that results in a stack overflow (from the terminal):

Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in <type 'exceptions.RuntimeError'> ignored

What is the correct way to do this? I tried a symlink per an answer on the linked question, but that resulted in the following:

me@mycomp:~/foo$ sudo ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/ django_admin
[sudo] password for me: 
ln: creating symbolic link `django_admin': Protocol error

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘寄 2024-10-23 02:17:52

递归错误是因为您正在用自身扩展 admin/index.html

您可以:

  • 复制 templates/admin/ 目录中的整个 admin/index.html 模板,它将用您的模板替换默认模板并
  • 覆盖 index .html 每个应用程序或模型,如解释这里

我知道问题提出后已经很晚了,但是你知道,谷歌旅行......

The recursion error is because you're extending the admin/index.html with itself.

You can either:

  • copy the entire admin/index.html template in your templates/admin/ directory, and it will replace the default template with yours
  • override the index.html per app or model, as explained here

I know this is late after the question, but you know, google traveling...

残疾 2024-10-23 02:17:52

使用额外的模板文件夹修改 settings.py,例如:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    "/home/mysite/webapps/django/myproject/templates",
    "/home/mysite/webapps/django/lib/python2.7/django/",  # extra folder
)

然后在 myproject/templates/admin 中添加您自己的 index.html,例如:

{% extends "contrib/admin/templates/admin/index.html" %}

{% block branding %}
    <h1 id="site-name">Administration for TheLittleButtonCo</h1>
{% endblock %}

显然,变化是可能的。这适用于 Django 1.3.1 Final

Amend settings.py with an extra template folder, for example:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    "/home/mysite/webapps/django/myproject/templates",
    "/home/mysite/webapps/django/lib/python2.7/django/",  # extra folder
)

Then in myproject/templates/admin add your own index.html like:

{% extends "contrib/admin/templates/admin/index.html" %}

{% block branding %}
    <h1 id="site-name">Administration for TheLittleButtonCo</h1>
{% endblock %}

Variations are possible, obviously. This works on Django 1.3.1 final

峩卟喜欢 2024-10-23 02:17:52

不确定您是否找到了答案,但您需要更改

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

为,

{% extends "admin/base_site.html" %}

因为这是原始 index.html 页面覆盖的内容。因为 Django 系统在使用默认的 admin 文件夹之前会搜索您的 templates 文件夹,所以在这种情况下它会在您的模板中找到 admin/index.html,然后它会尝试使用扩展来扩展自身(因此会出现递归错误)。

作为参考,您也可以在模板中自定义 base_site.html,它扩展了 base.html。最好的办法是复制原始文件:

/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/

并将其粘贴到模板文件夹中作为起点

Not sure if you found the answer, but you need to change

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

to

{% extends "admin/base_site.html" %}

as that is what the original index.html page overwrites. Because the Django system searches your templates folder before using the default admin one, so in this case it finds the admin/index.html in your templates, then it's trying to extend itself with the extend (hence the recursion error).

For reference you can customise the base_site.html in you templates too, it extends base.html. The best thing to do is copy the original from:

/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/

and paste it into your templates folder as a starting point

多情出卖 2024-10-23 02:17:52

我使用一个额外的包,名为 django-smart-extends

I use an extra package, called django-smart-extends

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文