我在几乎所有的网站上都看到了联系表单,它们让这一切看起来都很简单。阅读如何做到这一点似乎很复杂,特别是因为大多数解决方案都是使用php和服务器。我并不是很了解php,而且我的项目中已经有了四种语言,这让我不知所措。我想要一些简单的东西;填写详细信息,发送到电子邮件,并完成。
填写姓名:无名氏
填写道布:2018-06-11
发布于 2018-06-12 07:59:12
以下是如何实现这一目标的基本概述。
使用包含详细信息的表单:
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)
也许这会让你找到一条解决问题的正确道路。
https://stackoverflow.com/questions/50807493
复制相似问题