简体   繁体   English

django:HTML中的必填字段没有“ required”属性

[英]django: Required Field Not Having The “required” Attribute in HTML

I am new to django and I think it is fantastic so far. 我是django的新手,到目前为止,我认为这太棒了。 I faced this weird issue today: I have this FormModel: 我今天遇到了这个奇怪的问题:我有这个FormModel:

class RegisterForm2(UserCreationForm):
    mobile2 = forms.CharField(
        label=_('Mobile Confirmation'),
        max_length=50,
        required=True,
        widget = forms.TextInput(attrs = {'class':'nocopy'})
    )

    class Meta:
        model = User
        fields = ['username', 'mobile', 'mobile2', 'guardian_mobile']
        labels = {
            'username': _('Government ID'),
        }
        widgets = {
            # workaround since __init__ setting to required doesnt work
            'mobile': forms.TextInput(attrs = {'required':''}),
        }

    def __init__(self, *args, **kwargs):
        super(RegisterForm2, self).__init__(*args, **kwargs)

        self.fields['mobile'].required = True

Now mobile field has blank=True in the model but i want it to be required in the form only and so I did that in the __init__ . 现在,移动字段在模型中具有blank = True ,但我希望仅在表单中需要它,因此我在__init__中做到了。 Now it is making the field required but it is not adding required="" to the text input. 现在,它使该字段成为必填字段,但没有在文本输入中添加required =“” Now if i have overridden the field (and added the required=True ) like I did mobile2 field I wouldn't have this issue. 现在,如果像我对mobile2字段那样重写字段(并添加required = True ),我将不会遇到此问题。 This is a an issue because if i want the attribute required to show on fields i have to override the fields in the form to add the required=True which will go against the DRY principle. 这是一个问题,因为如果我希望所需的属性显示在字段中,则必须覆盖表单中的字段以添加required = True ,这将违反DRY原理。 Am I missing something or is this a bug of some sort? 我是否缺少某些东西,或者这是某种错误?

Edit: I failed to mention that I am using floppy forms. 编辑:我没有提到我正在使用软盘表格。 It could be related to that. 可能与此有关。 I have to investigate this issue further. 我必须进一步调查这个问题。

Note this answer assumes you are using regular Django forms, I wrote it before you mentioned that you were using floppy forms. 请注意,此答案假设您使用的是常规Django表单,我在提到您使用软盘表单之前就已编写了该表单。

Django does not currently add the required HTML 5 attribute when it renders form fields. Django在呈现表单字段时当前未添加required HTML 5属性。 Overriding the widget for the mobile field sounds looks OK to me. 在我看来,覆盖mobile字段声音的小部件看起来不错。 You are changing the widget, not duplicating the field, so I don't think you need to worry about it being DRY. 您在更改窗口小部件,而不是复制字段,因此我认为您不必担心它是DRY。

Note that setting required in the __init__ is having an effect - if you submit the form with an empty value for mobile , then you will get a validation error. 请注意, __init__所需的设置会产生影响-如果您提交的表中包含mobile的空值,则将收到验证错误。 Your mobile2 field will not have required in the rendered HTML unless you add it to its widget attrs. 除非您将mobile2字段添加到其小部件attrs中,否则在呈现的HTML中将required它。

Django will add the required attribute to form fields in Django 1.10, see ticket #22383 . Django将在Django 1.10中的表单字段中添加required属性,请参见故障单#22383

This is actuially possible in your __init__ . 实际上在__init__是可能的。 You just have to add the required attribute to the widget attributes: 您只需将所需属性添加到小部件属性中:

def __init__(self, *args, **kwargs): super(RegisterForm2, self).__init__(*args, **kwargs) self.fields['mobile'].required = True self.fields['mobile'].widget.attrs['required'] = 'required'

but i want it to be required in the form only 但我希望仅以表格形式要求

'mobile': forms.TextInput(attrs = {'required':''}),

You can do this in views.py also. 您也可以在views.py中执行此操作。 That's how I prefer to do it. 那就是我更喜欢这样做的方式。

form.fields['mobile'].widget.attrs['required'] = True 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM