【问题标题】:Django Template Form Fields show in a Disorder wayDjango模板表单字段以无序方式显示
【发布时间】: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


【解决方案1】:

您可以查看 django 表单文档以了解您的特定 django 版本: https://docs.djangoproject.com/en/1.11/topics/forms/#rendering-fields-manually

您可以单独访问每个字段并使用手动呈现

{{ form.field }}}

在您的模板中,您将按照您想要的顺序获取它们,而不是遍历表单集:

<form method="get">
  {% csrf_token %}
        <div class="row">
            <div class="col-md-12">
                <label class="col-4 control-label"> {{ form.Action.label }}               
                </label>
                {{ form.Action }}
            </div>
            <div class="col-md-12">
                <label class="col-4 control-label"> {{ form.model_id.label }}               
                </label>
                {{ form.model_id }}
            </div>
            <div class="col-md-12">
                <label class="col-4 control-label"> {{ form.Address.label }}               
                </label>
                {{ form.Address }}
            </div>
            <div class="col-md-12">
                <label class="col-4 control-label"> {{ form.Label.label }}               
                </label>
                {{ form.Label }}
            </div>
        </div>
        <button type="submit" class="btn btn-primary">
            <span class="glyphicon glyphicon-search"></span> Submit
        </button>
</form>

另外,我建议您为表单字段使用更具代表性的名称...

【讨论】:

    【解决方案2】:

    在 forms.py 中的 'meta' 类中,您可能已经在 { } 中设置了字段。 花括号用于无序的set。 在 [ ] 或 ( ) 中设置字段选项可帮助您按所需顺序获取它。

    例如:

    `class LoginForm(forms.ModelForm):
          username = forms.CharField(help_text='')
          password = forms.CharField(widget=forms.PasswordInput())
          class Meta:
                model = UserRegistration
                fields = ['username','password']`
    

    【讨论】:

      猜你喜欢
      • 2011-10-11
      • 2017-12-15
      • 2013-02-01
      • 2023-01-10
      • 2022-07-07
      • 2015-08-19
      • 1970-01-01
      • 2011-01-15
      • 2018-03-21
      相关资源
      最近更新 更多