在Django联系表格

时间:2014-04-14 23:14:00

标签: python django email send contact

我有一个联系表格,所有工作都没有任何错误,我唯一不明白的是,当我点击发送按钮没有收到任何消息时,有人可以告诉我为什么或出了什么问题,好吗? 我只有一个页面叫做联系,没有感谢页面! 感谢

这是我的代码: 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="info@example.com",
            recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS],
        )
        return super(ContactFormView, self).form_valid(form)

2 个答案:

答案 0 :(得分:1)

这样做有简短的方法,

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'),
        )

你不需要新的课程,只有这一点,你会看到0,1,2,3会识别每个选项,你可以在第一部分放置任何内容,例如: 0,1,2,3,或者#34; IHAQ" "我有一个问题"

STATUS_CHOICES = (
        ("0", ""),
        ("1", "I have a question"),
        ("2", "Help/Support"),
        ("3", "Please give me a call"),
        )

答案 1 :(得分:0)

此部分可能存在问题:

recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS],

如果settings.LIST_OF_EMAIL_RECIPIENTS已经是一个列表,那么您将它嵌套在另一个列表中。

无论如何,作为一般规则,当某些东西不起作用时,您应该在调试器中单步调试代码或者使用print语句来查看代码运行时发生的情况。这将使您的工作变得更加轻松。