Django Override Admin change_form.html Template - 在模板中显示关联模型

发布于 2024-09-27 09:00:31 字数 706 浏览 6 评论 0原文

我遇到过这样的情况:我覆盖了给定模型的 admin_change.html 模板。我创建了一个文件,例如:

/myproject/templates/admin/myapp/mymodel/change_form.html

然后,在重写的change_form.html 模板中,我正在编辑现有对象实例,我希望能够访问该模型实例变量,以便可以显示有关它的更多信息。

{% extends "admin/change_form.html" %}
{% block after_field_sets %}{{ block.super }}
Print my model here: {{ mymodel }}
Print foreignkey related records of my model:  
    {% for item in mymodel.items_set.all %} {{ item }} {% endfor %}
{% endblock %}

但是,我不知道应该使用什么来访问该模型的模板变量(如果它被传递的话)。我尝试过挖掘管理源代码,但很快就迷失了。有谁知道如何从扩展的 django 模板中访问此模型实例变量?

(注意:在上面的代码中,对 {{ mymodel }} 的引用是不正确的。但重点是我希望能够在模板代码中使用类似的变量来引用 mymodel 实例。)

任何建议都非常多赞赏。 谢谢, 乔

I have a situation where I've overrided the admin_change.html template for a given model. I've created a file such as:

/myproject/templates/admin/myapp/mymodel/change_form.html

Then, in the overrided change_form.html template, where I am editing an existing object instance, I want to have access to that model instance variable so I can display more information about it.

{% extends "admin/change_form.html" %}
{% block after_field_sets %}{{ block.super }}
Print my model here: {{ mymodel }}
Print foreignkey related records of my model:  
    {% for item in mymodel.items_set.all %} {{ item }} {% endfor %}
{% endblock %}

However, I don't know what the template variable is called that I should use to access this model (if it is even passed at all). I've tried digging through the admin source code, but get lost quickly. Does anyone know how to access this model instance variable from within an extended django template?

(NOTE: in the above code, the reference to {{ mymodel }} is incorrect. But the point is that I want to be able to use a variable like that in my template code to reference the mymodel instance.)

Any advice is much appreciated.
Thanks,
Joe

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

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

发布评论

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

评论(2

小嗲 2024-10-04 09:00:31

看起来我找到了一种使用这种语法来做到这一点的方法。

{% extends "admin/change_form.html" %}
{% block after_field_sets %}{{ block.super }}
Print my model here {{ original }}
Print foreignkey related records of my model:
{% for item in original.items_set.all %} {{ item }} {% endfor %}
{% endblock %}

很抱歉这么快回答,但感谢所有开始研究的人。也许有人会发现这很有帮助。如果您发现更好的方法,请随时发表评论。

Looks like I found a means to do this using this syntax.

{% extends "admin/change_form.html" %}
{% block after_field_sets %}{{ block.super }}
Print my model here {{ original }}
Print foreignkey related records of my model:
{% for item in original.items_set.all %} {{ item }} {% endfor %}
{% endblock %}

Sorry to answer so soon, but thanks to anyone who started researching. Perhaps someone will find this helpful. If you see a better way to do this, feel free to comment.

眸中客 2024-10-04 09:00:31

只是四处看看,似乎 {{adminform.form.instance}} 有效。

Just poking around, it seems that {{adminform.form.instance}} works.

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