Django Override Admin change_form.html Template - 在模板中显示关联模型
我遇到过这样的情况:我覆盖了给定模型的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来我找到了一种使用这种语法来做到这一点的方法。
很抱歉这么快回答,但感谢所有开始研究的人。也许有人会发现这很有帮助。如果您发现更好的方法,请随时发表评论。
Looks like I found a means to do this using this syntax.
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.
只是四处看看,似乎
{{adminform.form.instance}}
有效。Just poking around, it seems that
{{adminform.form.instance}}
works.