Django中的ModelForm浏览器中没有显示输入

时间:2015-09-23 16:45:32

标签: python django

当我实例化以下表单并在我的模板中使用它时。我的输入未按要求显示。任何人都知道为什么?

class SampleForm(forms.ModelForm):
  def __init__(self, *args, **kwargs):
    super(SampleForm, self).__init__(*args, **kwargs)
    # Making name required
    self.fields['x'].required = True
    self.fields['y'].required = True
    self.fields['z'].required = True
    self.fields['a'].required = True

  class Meta:
    model = Sample
    fields = ['x','y','z','a' 'email']

模板

<div>
  <form>
    {{form}}
  </form>
</div>

1 个答案:

答案 0 :(得分:0)

尝试使用:

{% if field.field.required %}

使用此代码段: snippet