1
我有ContactForm主題下拉使用枚舉,主題是三個不同的字符串: 1.我有一個問題。 2.幫助/支持 3.請給我打電話。主題從下拉菜單使用枚舉Django
當用戶發送的消息必須選擇以上三種之一,這是我下面的代碼:從django_enumfield進口枚舉
* forms.py *
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)
和像belwo一樣的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="[email protected]",
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沒有指定模型類。任何想法的傢伙? 謝謝
非常感謝您的幫助 – DilMac