【问题标题】:Django contact form confirmation emailDjango 联系表确认电子邮件
【发布时间】:2014-07-09 08:21:46
【问题描述】:

我对 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"

【问题讨论】:

    标签: python django django-forms django-views


    【解决方案1】:

    您可以通过cleaned_data 属性访问经过验证的表单数据(强制转换为相应类型的字段),如表单文档中所示 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())
    

    【讨论】:

    • 非常感谢!完美运行。
    猜你喜欢
    • 2018-04-15
    • 2022-07-06
    • 2020-12-07
    • 2011-05-30
    • 1970-01-01
    • 2016-07-02
    • 2020-05-05
    • 2011-09-09
    • 2015-06-26
    相关资源
    最近更新 更多