javascript - 将 html 表单发送到 Django 项目中的电子邮件地址的最佳方式?

标签 javascript python html django forms

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

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

最佳答案

以下是如何实现这一目标的基本概述。

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

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

在 View 中使用此表单,可能使用 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)

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

关于javascript - 将 html 表单发送到 Django 项目中的电子邮件地址的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50807493/

相关文章:

javascript - float 页脚仅在用户向下滚动 200 像素后出现

javascript - EACCES : permission denied, 打开 '/home/site/wwwroot/node_modules/.cache/.eslintcache'

javascript - d3 从 svg 元素中选择数据

python - SciPy:使用 b 向量数组的逐元素非负最小二乘法

python - 在 Cryptojs 中进行 AES 加密,在 Pycrypto 中进行解密

html - 按列将 html 表格折叠为单独的表格

javascript - 向正则表达式添加总字符数限制

python - 我可以在 Python 中使用多少 if/else 语句

html - 如何用白色描边制作圆 Angular 梯形?

html - 使网格项跨越到隐式网格中的最后一行/列