无限递归,同时扩展管理员的应用程序change_form模板
我在 template/admin/change_form.html
中有以下模板:
{% extends "admin/change_form.html" %}
{% block extrahead %}
{% include "dojango/base.html" %}
{% block dojango_content %}
{% endblock %}
{% endblock %}
但是由于某种原因它会抛出
TemplatesyntaxError: TemplateSyntaxError at /admin/cms/post/add/
Caught RuntimeError while rendering: maximum recursion depth exceeded while calling a Python object
I have the following template in template/admin/change_form.html
:
{% extends "admin/change_form.html" %}
{% block extrahead %}
{% include "dojango/base.html" %}
{% block dojango_content %}
{% endblock %}
{% endblock %}
However for some reason it throws a
TemplatesyntaxError: TemplateSyntaxError at /admin/cms/post/add/
Caught RuntimeError while rendering: maximum recursion depth exceeded while calling a Python object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现执行此操作的最佳方法是使用
'..'
向上移动几个目录,然后返回到只能在 Django 代码库中找到的目录。由于 Django 模板类似于
"django/contrib/admin/templates/admin"
,我发现这对我有用:如果这仍然导致与您拥有的其他结构发生冲突,您可以去进一步:
甚至:
虽然它有点 hacky,但至少通过执行上述操作,您不必使用涉及复制 django 源或设置符号链接的其他技术。
The best way to do this that I have found is to use
'..'
to go up a couple of directories, then go back down into directories that should only be found in the Django code base.As the Django templates are in something like
"django/contrib/admin/templates/admin"
, I found that this worked me:If that still causes a clash with some other structure you have, you could go further:
or even:
Although it is a little hacky, at least by doing the above you don't have to use some other technique that involves copying the django source or setting up a symlink.
对于 Django 核心来说这是不可能的。但这并非不可能。
复制并粘贴“原始模板并更改您不喜欢的内容”非常难看。
不要在模板中制作,无论你在 python 中没有制作什么
这个解决方案适用于任何模板:
http://pypi.python.org/pypi/django-smart-extends
With Django core it's impossible. But it is not impossible.
Copy and paste "the original template and change things you don't like" it's very ugly.
Don't make in the templates, whatever you don't make in python
This solution is to any template:
http://pypi.python.org/pypi/django-smart-extends
我知道已经晚了,但是...
如果扩展 - 这是比复制更好的选择 - 关键是将其命名为任何除了
/admin/ Change_form.html
。(虽然OP引用了
template/admin/change_form.html
,但这只是因为他的TEMPLATE_DIRS元组中的路径以'/template'结尾 - 我的通常以'/templates'结尾 - 但是,这些目录可以命名为任何名称并位于任何位置。)如果命名为
/admin//change_form.html
它将在每个应用程序上自动使用它将在每个应用程序上自动使用模型基础,如果命名为
/admin///change_form.html
如果在 ModelAdmin 中明确指定,则可以命名任何内容
最后,如果坚持将其命名为
/admin /change_form.html
,您可以 - 前提是extends
标记包含 django 安装的完整路径而不是相对路径。I know it's late, but...
If extending - which is a far better option than duplicating - the key is to have it named anything except
/admin/change_form.html
.(Although the OP referred to
template/admin/change_form.html
, this is simply because a path in his TEMPLATE_DIRS tuple ends in '/template' - mine generally end in '/templates' - but, these directories can be named anything and located anywhere.)It will be used automatically on a per-app basis if named
/admin/<MyAppName>/change_form.html
It will be used automatically on a per-model basis if named
/admin/<MyAppName>/<MyModelName>/change_form.html
It can be named anything if specified explicitly in the ModelAdmin
Finally, if insistent on naming it
/admin/change_form.html
, you can - provided that theextends
tag contains the full path to your django installation instead of a relative one.您位于
admin/change_form.html
中并扩展了admin/change_form.html
。您无法扩展您所在的同一模板。您可能希望如果您从管理应用程序覆盖模板,则可以扩展您覆盖的模板。但这不是它的工作原理。当您覆盖模板时,您将无法访问它。
解决您的问题的方法是复制原始模板并更改您不喜欢的内容。
You are in
admin/change_form.html
and you extendadmin/change_form.html
. You cannot extend the same template which you are in.You probably expected that if you override template from admin application, you can extend the one you override. But this is not how it works. When you override a template you cannot access it.
Solution to your problem is to copy original template and change things you don't like.
此外,您还可以使用
change_form_template
属性将您的AdminOptions
类指向另一个模板。类似于:
以及
myapp/my_change_form.html
:Also, you can point your
AdminOptions
class to another template using thechange_form_template
property.Something like:
And
myapp/my_change_form.html
:我也有同样的问题。通过将覆盖的模板放置在 myapp/templates/admin/myapp 而不是 myapp/templates/admin 下来解决。
I had the same problem. Solved by placing the overridden template under myapp/templates/admin/myapp instead of myapp/templates/admin.