Django EmailMessage send()工作,然后给出HTTP错误400:错误请求

时间:2017-01-21 00:16:41

标签: django django-forms django-templates django-views

我的客户网站上有两个表单;一个在主页上,另一个在联系页面上。我将在一分钟内测试表格并且它们工作正常,然后我将在稍后再次测试,我得到HTTP Error 400: Bad Request - 它让我疯狂,因为我无法理解它。我禁用其中一个表单进行故障排除,但这没有做任何事情。以下是我的表格;请告诉我,我错过了一些明显的东西:)

表单模板

<form action="" role="form" method="post" id="contactForm"> 
  {% csrf_token %} 
  {{ form.as_p }}
  <div class="form-group">
    <button class="btn btn-color-out btn-block" type="submit">Send Message</button>
  </div>
</form>

联络视图

def contact(request):
    form_class = ContactForm

    if request.method == 'POST':
        form = form_class(data=request.POST)
        messages.add_message(request, messages.SUCCESS, 'Thank you, your message was received.')

        if form.is_valid():
            fullname = request.POST.get('fullname', '')
            phone_number = request.POST.get('phone_number', '')
            email_address = request.POST.get('email_address', '')
            message_content = request.POST.get('message_content', '')
            subject = 'Contact Information Submitted from Trust and Beneficiary Advocates'
            from_email = settings.DEFAULT_FROM_EMAIL
            recipient_list = ['kfritz@*****.com', 'charles@*****.com']
            ctx = {
                'title': 'Contact Us',
                'subject1': subject,
                'fullname': fullname,
                'phone_number': phone_number,
                'email_address': email_address,
                'message_content': message_content
            }

            message = get_template('email_forms/contact_form_email.html').render(Context(ctx))
            msg = EmailMessage(subject, message, from_email=from_email, to=[email_address], bcc=recipient_list)
            msg.content_subtype = 'html'
            msg.send()

            return redirect('/thank-you/')
    return render(request, 'pages/contact.html', {
        'form': form_class, 'title': 'Contact Us'
    })

索引(主页)查看

def index(request):
    form_class = FooterForm

    if request.method == 'POST':
        form = form_class(data=request.POST)
        if form.is_valid():
            fullname = request.POST.get('fullname', '')
            phone_number = request.POST.get('phone_number', '')
            email_address = request.POST.get('email_address', '')
            message_content = request.POST.get('message_content', '')
            subject = 'Contact Information Submitted from Trust and Beneficiary Advocates'
            from_email = settings.DEFAULT_FROM_EMAIL
            recipient_list = ['kfritz@***.com', 'charles@***.com']
            ctx = {
                'title': 'Trust and Beneficiary Advocates',
                'subject': subject,
                'fullname': fullname,
                'phone_number': phone_number,
                'email_address': email_address,
                'message_content': message_content
            }

            message = get_template('email_forms/contact_form_email.html').render(Context(ctx))
            msg = EmailMessage(subject, message, from_email=from_email, to=[email_address], bcc=recipient_list)
            msg.content_subtype = 'html'
            msg.send()

            return redirect('/thank-you/')

    return render(request, 'pages/index.html', {
        'form': form_class, 'title': 'Trust and Beneficiary Advocates'
    })

谢谢。

1 个答案:

答案 0 :(得分:1)

您应该使用 msg.send(fail_silently = True)发送电子邮件,而不会将邮件发送到虚假或无效的电子邮件ID。

对于具有电子邮件发送功能的两个视图,请使用如下:

message = get_template('...../email_template.html').render(Context(ctx))
msg = EmailMessage(subject, message, from_email=from_email, to=[email_address], bcc=recipient_list)
msg.content_subtype = 'html'
msg.send(fail_silently = True)