我有一個聯繫表單,所有的工作都沒有任何錯誤,我唯一不明白的地方是當我點擊發送按鈕沒有消息接收時,有人能告訴我爲什麼或什麼是錯誤的嗎? 我只有一個叫做聯繫人的頁面,不用謝謝! 感謝django中的聯繫表格
這裏是我的代碼: models.py 從django.db進口車型
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="[email protected]",
recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS],
)
return super(ContactFormView, self).form_valid(form)
表單驗證?即是form_valid()被調用還是調用form_invalid()來代替? – rockingskier
從我的理解是被調用的有效表單。由於測試已經創建了聯繫人模型和下拉主題,因此我得到了該結果。這個想法是保存從表單中收到的所有電子郵件。 – DilMac