I have a contact form which all working without any error, the only thing I don't understand is when I click on send button no message receive, could anyone tell me why or what is wrong, please? I have only one page called contact, no thanks page! Thanks
Here is my code: models.py from django.db import models
class Subject(models.Model):
question_ = 0
question_one = 1
question_two = 2
question__three = 3
STATUS_CHOICES = (
(question_, ''),
(question_one, 'I have a question'),
(question_two, 'Help/Support'),
(question__three, 'Please give me a call'),
)
class Contact(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=150)
subject = models.CharField(choices=Subject.STATUS_CHOICES, default=1, max_length=100)
phone_number = models.IntegerField()
message = models.TextField()
def save(self, *args, **kwargs):
super(Contact, self).save(*args, **kwargs)
return 'Contact.save'
froms.py
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
import floppyforms as forms
from django_enumfield import enum
class SubjectEnum(enum.Enum):
question_ =0
question_one = 1
question_two = 2
question__three = 3
STATUS_CHOICES = (
(question_, ''),
(question_one, 'I have a question'),
(question_two, 'Help/Support'),
(question__three, 'Please give me a call'),
)
class ContactForm(forms.Form):
name = forms.CharField(required=True)
email = forms.EmailField(required=True)
subject = forms.TypedChoiceField(choices=SubjectEnum.STATUS_CHOICES, coerce=str)
phone_number = forms.IntegerField(required=False)
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)
views.py
from django.conf import settings
from django.core.mail import send_mail
from django.views.generic import FormView
from .forms import ContactForm
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="info@example.com",
recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS],
)
return super(ContactFormView, self).form_valid(form)
There may be an issue with this part:
If
settings.LIST_OF_EMAIL_RECIPIENTS
is already a list, then you are nesting it in another list.Anyway, as a general rule, when something doesn't work, you should step through the code in your debugger or put print statements to see what is happening when the code runs. That will make the process much easier for you.
There is short and better way of doing this,
You don't need new class, just this, where you see 0,1,2,3 is what will recognise each choice, you can put anything in the first section, e.g. 0,1,2,3, or "IHAQ" short for "I have a question"