如何从模板访问表单使用的模型实例? [英] How can i access the model instance used by a form from a template?
问题描述
我正在遍历模板中由模型表单构成的表单集。我想提供有关该模型的附加信息。如果对此如何在Django中从表单模板访问模型的答案问题将起作用,我可以这样做:
{%for formset.forms%}
状态:{{form._meta.model.status}}
{{form}}
{%endfor%}
但这只会引发TemplateSyntaxError:变量和属性可能不能以下划线开头。
<我认为那不是你想做的。模型是 class :它不会具有 status
状态,因为那是一个仅为特定实例获取值的字段。
我怀疑您的意思是访问与表单关联的模型 instance ,该模型只是 form.instance
。
I am iterating over a formset made of modelforms in my template. I want to provide aditional information on that model. If the answer to this How to Access model from Form template in Django question would work, i could do this:
{% for form in formset.forms %}
Status:{{ form._meta.model.status }}
{{form}}
{% endfor %}
But that just throws the TemplateSyntaxError: Variables and attributes may not begin with underscores.
I don't think that's what you want to do. A model is a class: it won't have a status
, as that's a field which only gets a value for a particular instance.
I suspect what you mean to do is access the model instance associated with the form, which is just form.instance
.
这篇关于如何从模板访问表单使用的模型实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!