I have ContactForm with subject dropdown using enum, the subject is three different string: 1. I have a question. 2. Help/Support 3. Please give me a call.
When the user send a message have to select one of the above three, here is my code below:
*forms.py*
from django_enumfield import enum
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)
And view.py file like belwo:
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)
Contact 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 %}
I get an error say, ValueError: ModelForm has no model class specified. Any idea guys? Thanks