Django forms.DateInput不适用于attrs字段中给出的属性 [英] Django forms.DateInput does not apply the attributes given in attrs field

查看:961
本文介绍了Django forms.DateInput不适用于attrs字段中给出的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

占位符,尝试通过django的attrs说明符 forms.DateInput

Placeholder, class not getting set when tried to apply through the django's attrs specifier for forms.DateInput

该表单是 ModelForm

根据 docs


作为TextInput,还有一个可选参数:

Takes same arguments as TextInput, with one more optional argument:

这是代码:

widgets = {
   'my_date_field': forms.DateInput(format=('%d-%m-%Y'), 
                    attrs={'class':'myDateClass', 
                           'placeholder':'Select a date'}
                    )
}

同样适用于表单

The same is applied for a forms.TextInput and it works just fine.

我在这里缺少什么?

只要有人想要一个完整的类代码:

Just anybody wants a full class code :

class trademark_form(ModelForm):
    my_date_field = DateField(input_formats=['%d-%m-%Y'])
    class Meta:
        model = myModel

        widgets = {
                   'my_date_field': forms.DateInput(format=('%d-%m-%Y'), attrs={'class':'myDateClass', 'placeholder':'Select a date'}),
                   'field1': forms.TextInput(attrs={'class':'textInputClass', 'placeholder':'Enter a Value..'}),
                   'field2': forms.TextInput(attrs={'class':'textInputClass', 'placeholder':'Enter a Value..', 'readonly':'readonly', 'value':10}),
                   'desc': forms.Textarea(attrs={'class':'textAreaInputClass', 'placeholder':'Enter desc', 'rows':5}),

               }
        exclude = ('my_valid_field')

该字段生成的HTML my_date_field

The generated HTML for the field, my_date_field :

<input type="text" id="id_my_date_field" name="my_date_field">

为字段生成的HTML field1

The generated HTML for the field, field1 :

<input type="text" name="field1" class="textInputClass" placeholder="Enter a Value.." id="id_field1">


推荐答案

由于您没有发布您的表单代码,我最好的猜测是你明确地实例化了一个这样的表单字段通过发布大致如下的代码证实了我的猜测:

Since you didn't post your form code, my best guess is that you explicitly instantiated a form field like this confirmed my guess by posting the code that looks roughly like this:

class MyForm(forms.ModelForm):
    my_date_field = forms.DateField()

    class Meta:
        model = MyModel
        widgets = {
            'my_date_field': forms.DateInput(format=('%d-%m-%Y'), 
                                             attrs={'class':'myDateClass', 
                                            'placeholder':'Select a date'})
        }

我可以说这不工作,因为您明确地实例化一个表单域,Django假定您要完全定义表单字段行为;因此,您不能使用内部 Meta 类的小部件属性。

I can say that it's not working because if you explicitly instantiate a form field like this, Django assumes that you want to completely define form field behavior; therefore, you can't use the widgets attribute of the inner Meta class.

关于覆盖默认字段类型或小部件指出:


声明性地定义的字段保持原样因此,对Meta属性(例如小部件,标签,
help_texts或error_messages)所做的任何
自定义都将被忽略;这些仅适用于自动生成的字段

Fields defined declaratively are left as-is, therefore any customizations made to Meta attributes such as widgets, labels, help_texts, or error_messages are ignored; these only apply to fields that are generated automatically.

这篇关于Django forms.DateInput不适用于attrs字段中给出的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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