Django 不会向自定义 ModelForm 小部件添加属性 [英] Django does not add attribute to the custom ModelForm widget

查看:24
本文介绍了Django 不会向自定义 ModelForm 小部件添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型.py

class MyModel(models.Model):pub_date = models.DateTimeField(默认=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()元类:模型 = 模型.MyModel字段 = ['title', 'text', 'tos']小部件 = {'title': TextInput(attrs={'class': 'form-control', 'placeholder': 'Title'}),'text': Textarea(attrs={'class': 'form-control', 'placeholder': 'Text'}),'tos': CheckboxInput(attrs={'data-validation-error-msg': '您必须同意我们的条款和条件'}),}

结果:

<预><代码>>>>打印(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>

您可以看到在 TOS 字段中 data-validation-error-msg 属性缺失.

有什么想法吗?

编辑

这有效:

class MyModelForm(ModelForm):tos = BooleanField(小部件=复选框输入(attrs={'data-validation-error-msg': '您必须同意我们的条款和条件'}))元类:模型 = 模型.MyModel字段 = ['title', 'text', 'tos']小部件 = {'title': TextInput(attrs={'class': 'form-control', 'placeholder': 'Title'}),'text': Textarea(attrs={'class': 'form-control', 'placeholder': 'Text'}),}

它不能与 Meta 类一起工作仍然很奇怪.

解决方案

widgets 选项用于覆盖默认值.它不适用于您的 tos 字段,因为您已在表单中声明了 tos = BooleanField().请参阅widgets 文档 有关这方面的更多信息.

您可以通过在声明 tos 字段时传递 widget 来解决此问题:

class MyModelForm(ModelForm):tos = BooleanField(widget=CheckboxInput(attrs={'data-validation-error-msg': '您必须同意我们的条款和条件'}))元类:模型 = 模型.MyModel字段 = ['title', 'text', 'tos']小部件 = {'title': TextInput(attrs={'class': 'form-control', 'placeholder': 'Title'}),'text': Textarea(attrs={'class': 'form-control', 'placeholder': 'Text'}),}

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.

解决方案

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'}),
        }

这篇关于Django 不会向自定义 ModelForm 小部件添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆