且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django模板:表单字段名称作为变量?

更新时间:2023-02-19 10:56:14

您不能在django模板中传递参数,所以不行。

You can't pass arguments in the django template, so no.

您需要实现一个模板标签(更多的是痛苦)或过滤,或者从票据 to formfield 在你的意见(我会推荐什么)。

You'd need to implement a template tag (more of a pain) or filter, or make the association from ticket to formfield in your view (what I would recommend).

由于我实际上不知道票之间的关系和您的表单字段,我无法确定***的方法是什么,但这将根据您提供的信息正常工作。 p>

Since I don't actually know the relationship between ticket and your form fields, I can't tell what the best method is, but this would 'just work' based on the info you've provided.

# view
form = MyForm()
for ticket in zone.tickets:
    ticket.form_field = form['ticket_count_' + str(ticket.id)]

# template
{% for ticket in zone.tickets %}
    {{ ticket.id }} : {{ ticket.form_field }}
{% endfor %}