在模板中,我正确显示了整个 DropDown,如下所示:
{{form.deal_type}}
Run Code Online (Sandbox Code Playgroud)
但是,如果我只想显示所选下拉列表的文本怎么办?
这显示我只是一个外键。
{{form.deal_type.value}}
Run Code Online (Sandbox Code Playgroud)
我不知道你为什么要这么做,但试试这个。
{% for value, text in form.deal_type.field.choices %}
{{ value }}: {{ text }}
{% if value == form.deal_type.value %}
<strong>{{ text }}</strong> <!-- THIS IS THE SELECTED ONE... -->
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我的意思是上面的代码只是为了说明,而不是说您应该逐字使用它。这段代码会做的更像你想要的。
{{ form.deal_type.label_tag }}
{% for value, text in form.deal_type.field.choices %}
{% if value == form.deal_type.value %}
{{ text }}
<input type="hidden" name="deal_type" value="{{ value }}" />
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6649 次 |
| 最近记录: |