【发布时间】:2019-04-05 10:41:18
【问题描述】:
我正在处理 Django Form,所以基本上我有四个表单字段,例如:
First Name:
Last Name:
Address:
Contact Info:
此表单数据进入 Django 模板和渲染,但显示这些字段的顺序不正确。每次刷新页面表单字段顺序都会发生变化。 例子: 在第一页加载它的样子
Last Name:
First Name:
Contact Info:
Address:
关于刷新页面顺序变化之类的
Address:
Last Name:
Contact Info:
First Name:
这是我在 Tempalte 中呈现表单的 Django 模板表单代码:
{% for fields in formset %}
<div class="row">
<div class="col-md-12">
<label class="col-4 control-label">{{fields.label}}
</label>
{{fields}}
</div>
</div>
{% endfor %}
我的 Form.py 文件包含这个表单:
class my_info(forms.Form):
Action = forms.ChoiceField(
label="First Name",
widget=forms.Select(attrs={'class': 'form-control'}),
choices=Action_choice)
model_id = forms.CharField(
label ='Last Name',
widget=forms.TextInput(attrs={'class': 'form-control','type':'hidden'})
)
Address = forms.CharField(
label ='Address',
widget=forms.TextInput(attrs={'class': 'form-control'})
) ,
Label = forms.CharField(
label ='Contact Info',
widget=forms.TextInput(attrs={'class': 'form-control'})
)
但我想以正确的顺序显示这些字段。
First Name:
Last Name:
Address:
Contact Info:
【问题讨论】:
-
你能分享你的
Form代码吗?由于属性在 Python-2.7 中没有排序(这些存储在字典中,并且没有排序),因此必须明确指定顺序。 -
使用表单代码更新问题。能否请您描述一下如何指定订单。实际上,在呈现表单时,它会按完整顺序呈现,但是当它显示在页面顺序改变时
-
你用的是什么 Django 版本?
-
我使用的是 Django 版本 1.11.10 和 python 2.7
标签: html django python-2.7 django-forms django-templates