我在django表单和模板视图中遇到了一个有趣的错误。
在我的views.py中,如果我包含以下视图,我收到http 500服务器错误,但电子邮件发送成功
class ContactUsView(generic.FormView):
template_name = 'contact.html'
form_class = ContactForm
success_url = "/"
def form_valid(self,form):
message = "{name} / {email_addr} said: ".format(name=form.cleaned_data.get('name'),email_addr=form.cleaned_data.get('email_addr'))
message += "\n\n{0}".format(form.cleaned_data.get('inquiry'))
send_mail(subject=form.cleaned_data.get('subject').strip(),message=message,from_email='sender@working.email.com',
recipient_list=['receiver@example.com'],
)
return super(ContactView, self).form_valid(form)
如果我将上述代码更改为以下代码,我会被重定向到相应的成功页面,但是电子邮件无法发送,是否有我遗漏的内容?
class ContactUsView(generic.FormView):
template_name = 'contact.html'
form_class = ContactForm
success_url = "/"
def is_valid(self,form):
message = "{name} / {email_addr} said: ".format(name=form.cleaned_data.get('name'),email_addr=form.cleaned_data.get('email_addr'))
message += "\n\n{0}".format(form.cleaned_data.get('inquiry'))
send_mail(subject=form.cleaned_data.get('subject').strip(),message=message,from_email='sender@working.email.com',
recipient_list=['receiver@example.com'],
)
return super(ContactView, self).is_valid(form)
我觉得我有一些基本的东西,但是我不确定它是什么。