我试图创建一个窗体,我从用户那里得到一些细节。Django错误:TypeError:__init __()得到了一个意外的关键字参数'attrs'
我已经定义在forms.py
领域,我使用Django widget
系统还定义诸如占位和CSS类其他属性。但它显示了我TypeError
:
TypeError: __init__() got an unexpected keyword argument 'attrs'
以下是我的代码:
models.py
from django.db import models
class Contact(models.Model):
your_name = models.CharField(max_length=100)
your_email = models.EmailField()
your_subject = models.CharField(max_length=100)
your_comment = models.TextField(max_length=200)
def __str__(self):
return self.name
forms.py
from django.forms import ModelForm, TextInput, TextInput, EmailField
from .models import Contact
class ContactForm(ModelForm):
class Meta:
model = Contact
fields = ('your_name', 'your_email', 'your_subject', 'your_comment')
widgets = {
'your_name' : TextInput(attrs={'placeholder': 'Name *', 'class': 'form-control'}),
'your_email' : EmailField(attrs={'placeholder': 'Email *', 'class': 'form-control'}),
'your_subject' : TextInput(attrs={'placeholder': 'Subject *', 'class': 'form-control'}),
'your_comment' : Textarea(attrs={'placeholder': 'Comment *', 'class': 'form-control'}),
}
我已阅读Django docs for Overriding default fields也是这个问题init() got an unexpected keyword argument 'attrs'但无法修复错误。
我对Python和Django来说很新,希望得到任何帮助,谢谢。
我已经放在'EmailField'声明'widget'只或可能是我已经做了不正确的方式。 –
是的,这就是问题所在。 EmailField是一个带有小部件的“字段”。它不能被设置为一个小部件。 – Pythonista