我有使用枚举的主题下拉列表的 ContactForm,主题是三个不同的字符串: 1. 我有一个问题。2. 帮助/支持 3. 请给我打电话。
当用户发送消息时必须选择上述三个之一,这是我的代码如下:
*表格.py *
从 django_enumfield 导入枚举
class SubjectEnum(enum.Enum):
subject_one = 'I have a question'
subject_two = 'Help/Support'
subject_three = 'Please give me a call'
class ContactForm(forms.ModelForm):
name = forms.CharField(required=True)
email = forms.EmailField(required=True)
subject = forms.TypedChoiceField(choices=SubjectEnum.choices(), coerce=str)
message = forms.CharField(widget=forms.Textarea)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.add_input(Submit('submit', 'Submit'))
super(ContactForm, self).__init__(*args, **kwargs)
像下面这样的 view.py 文件:
class ContactFormView(FormView):
form_class = ContactForm
template_name = "contact/email_form.jade"
success_url = '/email-sent/'
def form_valid(self, form):
message = "{name} / {email} said: ".format(
name=form.cleaned_data.get('name'),
email=form.cleaned_data.get('email'))
message += "\n\n{0}".format(form.cleaned_data.get('message'))
send_mail(
subject=form.cleaned_data.get('-subject').strip(),
message=message,
from_email="enquiries@example.com",
recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS],
)
return super(ContactFormView, self).form_valid(form)
联系表:
- extends "base.jade"
- load crispy_forms_tags
block meta_title
| Contact Us
block content
.jumbotron
h1 Contact Us
.row
.span6
{% crispy form form.helper %}
我收到一条错误消息,ValueError: ModelForm has no model class specified。有什么想法吗?谢谢