无限递归,同时扩展管理员的应用程序change_form模板

发布于 2024-10-16 17:32:05 字数 459 浏览 3 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(6

暖风昔人 2024-10-23 17:32:06

我发现执行此操作的最佳方法是使用 '..' 向上移动几个目录,然后返回到只能在 Django 代码库中找到的目录。

由于 Django 模板类似于 "django/contrib/admin/templates/admin",我发现这对我有用:

{% extends "../../admin/templates/admin/change_form.html" %}

如果这仍然导致与您拥有的其他结构发生冲突,您可以去进一步:

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

甚至:

{% extends "../../../../django/contrib/admin/templates/admin/change_form.html" %}

虽然它有点 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:

{% extends "../../admin/templates/admin/change_form.html" %}

If that still causes a clash with some other structure you have, you could go further:

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

or even:

{% extends "../../../../django/contrib/admin/templates/admin/change_form.html" %}

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.

伪心 2024-10-23 17:32:06

对于 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

灼痛 2024-10-23 17:32:05

我知道已经晚了,但是...

如果扩展 - 这是比复制更好的选择 - 关键是将其命名为任何除了 /admin/ Change_form.html

(虽然OP引用了template/admin/change_form.html,但这只是因为他的TEMPLATE_DIRS元组中的路径以'/template'结尾 - 我的通常以'/templates'结尾 - 但是,这些目录可以命名为任何名称并位于任何位置。)

如果命名为 /admin//change_form.html 它将在每个应用程序上自动使用

它将在每个应用程序上自动使用模型基础,如果命名为 /admin///change_form.html

如果在 ModelAdmin 中明确指定,则可以命名任何内容

class MyModelAdmin(admin.ModelAdmin):
    change_form_template = 'subdir/my_change_form.html'

最后,如果坚持将其命名为 /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

class MyModelAdmin(admin.ModelAdmin):
    change_form_template = 'subdir/my_change_form.html'

Finally, if insistent on naming it /admin/change_form.html, you can - provided that the extends tag contains the full path to your django installation instead of a relative one.

我一直都在从未离去 2024-10-23 17:32:05

您位于 admin/change_form.html 中并扩展了 admin/change_form.html。您无法扩展您所在的同一模板。

您可能希望如果您从管理应用程序覆盖模板,则可以扩展您覆盖的模板。但这不是它的工作原理。当您覆盖模板时,您将无法访问它。

解决您的问题的方法是复制原始模板并更改您不喜欢的内容。

You are in admin/change_form.html and you extend admin/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.

清引 2024-10-23 17:32:05

此外,您还可以使用 change_form_template 属性将您的 AdminOptions 类指向另一个模板。

类似于:

class MyOptions(AdminOptions):
    change_form_template = 'myapp/my_change_form.html'

以及myapp/my_change_form.html

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

Also, you can point your AdminOptions class to another template using the change_form_template property.

Something like:

class MyOptions(AdminOptions):
    change_form_template = 'myapp/my_change_form.html'

And myapp/my_change_form.html:

{% extends "admin/change_form.html" %}
冧九 2024-10-23 17:32:05

我也有同样的问题。通过将覆盖的模板放置在 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.

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