Django联系表格确认电子邮件

时间:2014-05-20 21:45:57

标签: python django django-forms django-views

我是django的新手并且正在构建我的第一个应用程序。

尝试搜索网站,但是对于我的生活找不到所需的相关信息。

我希望在联系表单上将确认电子邮件发送到输入的电子邮件地址。我已经看到了发送到选定地址或用户的示例,但我似乎无法确定如何将邮件发送到表单上输入的电子邮件。

非常感谢任何帮助!

models.py

from django.db import models

class Quote(models.Model):
    name = models.CharField(max_length=200, blank=False, null=False, verbose_name="your name")
    email = models.EmailField(max_length=255, blank=False, null=False)

    created_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.name

forms.py

class QuoteForm(forms.ModelForm):
    class Meta:
        model = Quote

views.py

class QuoteView(CreateView):
    model = Quote
    form_class = QuoteForm
    template_name = "quote/quote.html"
    success_url = "/quote/success/"

    def form_valid(self, form):
        super(QuoteView,self).form_valid(form)
        return HttpResponseRedirect(self.get_success_url())

class QuoteSuccessView(TemplateView):
    template_name = "quote/quote-complete.html"

1 个答案:

答案 0 :(得分:1)

您可以通过cleaned_data属性访问经过验证的表单数据(强制转换为相应类型的字段),如表格docs所示 https://docs.djangoproject.com/en/dev/topics/forms/#processing-the-data-from-a-form

from django.core.mail import send_mail


def form_valid(self, form):
    super(QuoteView,self).form_valid(form)
    send_mail("Foo", "bar", 'from@example.com', [form.cleaned_data['email']])
    return HttpResponseRedirect(self.get_success_url())