【问题标题】:Best way to send html form to an email address within Django project?将 html 表单发送到 Django 项目中的电子邮件地址的最佳方式?
【发布时间】:2018-06-11 23:48:29
【问题描述】:

我在几乎所有网站上都看到了联系表格,它们看起来很简单。阅读如何做到这一点似乎很复杂,特别是因为大多数解决方案都使用 php 和服务器。我真的不知道 php,而且我的项目中已经有四种语言,而且它变得不堪重负。我想要一些简单的东西;填写详细信息,发送到电子邮件,然后完成。

填写姓名:John Doe
填写出生日期:2018 年 6 月 11 日
[提交](发送至 example@example.com)

【问题讨论】:

    标签: javascript python html django forms


    【解决方案1】:

    这里是如何实现的基本概述。

    使用包含详细信息的表单:

    from django import forms
    class ContactForm(forms.Form):
        name = ...
        birht_date = ...
    

    在视图中使用此表单,可能使用FormView

    from django.conf import settings
    from django.core.mail import send_mail
    from django.views.generic.edit import FormView
    
    class ContactView(FormView):
        form_class = ContactForm
        success_url = '/thanks/'
    
        def form_valid(self, form):
            # here you send the email
            send_email(
                sub='New contact: {}'.format(form.cleaned_data['name']),
                msg='This new contact was born  {}'.format(form.cleaned_data['birth_date']),
                from=settings.SERVER_EMAIL,
                to='example@example.com')
    
            return super().form_valid(form)
    

    也许这会让您找到解决问题的正确途径。

    【讨论】:

    • 谢谢。我会试试这个解决方案。
    猜你喜欢
    • 2016-10-04
    • 2012-11-14
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2014-10-30
    • 2013-04-28
    相关资源
    最近更新 更多