难以覆盖 Django 管理模板
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
递归错误是因为您正在用自身扩展
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:
admin/index.html
template in yourtemplates/admin/
directory, and it will replace the default template with yoursindex.html
per app or model, as explained hereI know this is late after the question, but you know, google traveling...
使用额外的模板文件夹修改 settings.py,例如:
然后在 myproject/templates/admin 中添加您自己的 index.html,例如:
显然,变化是可能的。这适用于 Django 1.3.1 Final
Amend settings.py with an extra template folder, for example:
Then in myproject/templates/admin add your own index.html like:
Variations are possible, obviously. This works on Django 1.3.1 final
不确定您是否找到了答案,但您需要更改
为,
因为这是原始 index.html 页面覆盖的内容。因为 Django 系统在使用默认的 admin 文件夹之前会搜索您的 templates 文件夹,所以在这种情况下它会在您的模板中找到 admin/index.html,然后它会尝试使用扩展来扩展自身(因此会出现递归错误)。
作为参考,您也可以在模板中自定义
base_site.html
,它扩展了base.html
。最好的办法是复制原始文件:并将其粘贴到模板文件夹中作为起点
Not sure if you found the answer, but you need to change
to
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 extendsbase.html
. The best thing to do is copy the original from:and paste it into your templates folder as a starting point
我使用一个额外的包,名为 django-smart-extends
I use an extra package, called django-smart-extends