Django does not add attribute to the custom ModelForm widget

こ雲淡風輕ζ 提交于 2019-12-20 07:29:19

问题


models.py

class MyModel(models.Model):
    pub_date = models.DateTimeField(default=timezone.now)
    title = models.CharField(max_length=255, blank=False, null=False)
    text = models.TextField(blank=True, null=True)

forms.py

class MyModelForm(ModelForm):
    tos = BooleanField()
    class Meta:
        model = models.MyModel
        fields = ['title', 'text', 'tos']
        widgets = {
            'title': TextInput(attrs={'class': 'form-control', 'placeholder': 'Title'}),
            'text': Textarea(attrs={'class': 'form-control', 'placeholder': 'Text'}),
            'tos': CheckboxInput(attrs={'data-validation-error-msg': 'You have to agree to our terms and conditions'}),
        }

Results:

>>> print(forms.MyModelForm())
<tr><th><label for="id_title">Title:</label></th><td><input type="text" name="title" class="form-control" placeholder="Title" maxlength="255" required id="id_title" /></td></tr>
<tr><th><label for="id_text">Text:</label></th><td><textarea name="text" cols="40" rows="10" class="form-control" placeholder="Text" id="id_text"></textarea></td></tr>
<tr><th><label for="id_tos">Tos:</label></th><td><input type="checkbox" name="tos" required id="id_tos" /></td></tr>

You can see that in the TOS field data-validation-error-msg attribute is missing.

Any ideas?

EDIT

This works:

class MyModelForm(ModelForm):
    tos = BooleanField(
        widget=CheckboxInput(
            attrs={'data-validation-error-msg': 'You have to agree to our terms and conditions'}))
    class Meta:
        model = models.MyModel
        fields = ['title', 'text', 'tos']
        widgets = {
            'title': TextInput(attrs={'class': 'form-control', 'placeholder': 'Title'}),
            'text': Textarea(attrs={'class': 'form-control', 'placeholder': 'Text'}),
        }

It's still weird that it didn't work with the Meta class.


回答1:


The widgets option is for overriding the defaults. It doesn't work for your tos field because you have declared tos = BooleanField() in your form. See the note in the widgets docs for more information about this.

You can fix the issue by passing widget when you declare the tos field:

class MyModelForm(ModelForm):
    tos = BooleanField(widget=CheckboxInput(attrs={'data-validation-error-msg': 'You have to agree to our terms and conditions'}))
    class Meta:
        model = models.MyModel
        fields = ['title', 'text', 'tos']
        widgets = {
            'title': TextInput(attrs={'class': 'form-control', 'placeholder': 'Title'}),
            'text': Textarea(attrs={'class': 'form-control', 'placeholder': 'Text'}),
        }


来源:https://stackoverflow.com/questions/47558927/django-does-not-add-attribute-to-the-custom-modelform-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!