【问题标题】:How to remove required from Django-Widget-Tweaks form field如何从 Django-Widget-Tweaks 表单字段中删除所需
【发布时间】:2020-06-06 21:33:42
【问题描述】:

我正在尝试从 django-widget-tweaks 表单中删除所需的属性。

我试过了:

{% render_field form.legal_entity|attr:'required:false' placeholder=form.legal_entity.label class+="form-control" %}

{% render_field form.legal_entity|remove_attr:"required" placeholder=form.legal_entity.label class+="form-control" %}

无论我做什么,结果总是:

<input type="text" name="legal_entity" maxlength="120" class="form-control" placeholder="Firmenname" required id="id_legal_entity">

这是相应的表格:

class MerchantForm(forms.ModelForm):

    class Meta:
        model = Merchant
        fields = ['name', 'legal_entity', 'legal_address','legal_zip', 'legal_city','address', 'zip', 'city', 'contact_person', 'phone', 'email']

 def clean_legal_entity(self):
        data = self.cleaned_data['legal_entity']
        return data

...

【问题讨论】:

  • 你能分享一下 Django 表单吗?
  • 查看问题的编辑。

标签: django django-widget-tweaks


【解决方案1】:

您可以通过在相应字段中设置required=False [Django-doc]来将该字段标记为非必填:

class MerchantForm(forms.ModelForm):
    legal_entity = forms.CharField(required=False)

    class Meta:
        model = Merchant
        fields = ['name', 'legal_entity', 'legal_address','legal_zip', 'legal_city','address', 'zip', 'city', 'contact_person', 'phone', 'email']

【讨论】:

    【解决方案2】:

    在您的 HTML 页面的 JS 代码中:

     window.onload=myfunction();
     function myfunction()
    {
        $("input").removeAttr("required");
        $("select").removeAttr("required");
        $("textarea").removeAttr("required");
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 2019-04-01
      • 2019-08-28
      • 2016-09-19
      • 1970-01-01
      • 2017-06-25
      • 2012-12-20
      • 2016-01-30
      • 2018-05-04
      相关资源
      最近更新 更多