2

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

1 Answer 1

2

You should specify a Meta class inside your model form and set the model used. Note that you don't need to specify the form fields that are not different from your model's fields.

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)

    class Meta:
        model = Contact
        fields = ['name', 'email', 'subject', 'message']

If you don't have a model Contact, you should be using a Form instead of a ModelForm:

class ContactForm(forms.Form):
    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)

See the documentation for more information on ModelForms and their use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.