我在几乎所有网站上都看到了联系表单,它们看起来很简单。阅读如何做到这一点似乎很复杂,特别是因为大多数解决方案都使用 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/