Django Override Admin change_form.html模板 - 在模板中显示关联的模型 [英] Django Override Admin change_form.html Template - display associated model in template

查看:1276
本文介绍了Django Override Admin change_form.html模板 - 在模板中显示关联的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我已经覆盖给定模型的admin_change.html模板。我创建了一个文件,如:

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

然后,在覆盖的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 %}

但是,我不知道调用模板变量,我应该使用它来访问这个模型(如果它甚至被传递)。我试过挖掘管理员的源代码,但很快就会迷路。有没有人知道如何从扩展django模板中访问此模型实例变量?

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?

(注意:在上面的代码中,对{{mymodel}}的引用是不正确的,但关键是我想要使用像这样的变量在我的模板代码中引用mymodel实例。)

(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.)

任何建议都非常感谢。
谢谢,
Joe

Any advice is much appreciated. Thanks, Joe

推荐答案

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

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.

这篇关于Django Override Admin change_form.html模板 - 在模板中显示关联的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆