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

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

问题描述

models.py

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

结果:

>>> 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>

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

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

有什么想法吗?

编辑

EDIT

这有效:

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

仍然很奇怪在 Meta 类中不起作用。

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

推荐答案

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

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.

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

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天全站免登陆