Django/Sendgrid 表单 HTTP 错误 400:错误请求
Django/Sendgrid Form HTTP Error 400: Bad Request
当我尝试使用 Sendgrid 发送我的 Django 表单时出现 HTTPError: HTTP Error 400: Bad Request
错误。我已经确认 envvars 中有一个 Sendgrid 密钥。这是我的代码:
settings.py
# sendgrid
EMAIL_BACKEND = "sgbackend.SendGridBackend"
SENDGRID_API_KEY = get_env_variable('SENDGRID_KEY')
urls.py
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from search import views as search_views
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.contrib.wagtailsitemaps.views import sitemap
urlpatterns = [
url(r'^django-admin/', include(admin.site.urls)),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
url('^sitemap\.xml$', sitemap),
url(r'', include(wagtail_urls)),
]
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Serve static and media files from development server
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
contact_form.html
{% load static %}
{% load app_tags %}
<div id="contact-form">
<p class="thank-you" style='display:none;color:#ee9836;'>Thank you for your message. We'll be in contact with you soon.</p>
<form role="form" action="" method="post" class="form panel-body">
{% csrf_token %}
{{ form }}
<button class="btn btn-primary pull-right" type="submit">Submit</button>
</form>
</div>
我的表单视图在模板标签中:
app_tags.py
from django import template
from django.shortcuts import render, redirect
from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context
from .forms import ContactForm
register = template.Library()
# contact form for footer
@register.inclusion_tag('contact_form.html')
def contact(request):
form = ContactForm
if request.method == 'POST':
contact_form = form(data=request.POST)
if contact_form.is_valid():
contact_name = request.POST.get('name', '')
contact_email = request.POST.get('email', '')
contact_message = request.POST.get('message', '')
# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = Context({
'contact_name': contact_name,
'contact_email': contact_email,
'contact_message': contact_message,
})
content = template.render(context)
print content
email = EmailMessage(
"Contact Form",
content,
"contact@site.com" +'',
['email@sample.com'],
headers = {'Reply-To': contact_email }
)
email.send()
else:
contact_form = form()
return {'form': form}
contact_template.txt
Contact Name:
{{ contact_name }}
Email:
{{ contact_email }}
Message:
{{ contact_message }}
这里有一些回溯:
return opener.open(request)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request
尝试使用 reply_to kwarg 而不是在 headers 中传递它。
例如:
email = EmailMessage(
"Contact Form",
content,
"contact@site.com",
['email@sample.com'],
reply_to=[contact_email],
)
https://docs.djangoproject.com/en/1.10/topics/email/#emailmessage-objects
当我尝试使用 Sendgrid 发送我的 Django 表单时出现 HTTPError: HTTP Error 400: Bad Request
错误。我已经确认 envvars 中有一个 Sendgrid 密钥。这是我的代码:
settings.py
# sendgrid
EMAIL_BACKEND = "sgbackend.SendGridBackend"
SENDGRID_API_KEY = get_env_variable('SENDGRID_KEY')
urls.py
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from search import views as search_views
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.contrib.wagtailsitemaps.views import sitemap
urlpatterns = [
url(r'^django-admin/', include(admin.site.urls)),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
url('^sitemap\.xml$', sitemap),
url(r'', include(wagtail_urls)),
]
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Serve static and media files from development server
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
contact_form.html
{% load static %}
{% load app_tags %}
<div id="contact-form">
<p class="thank-you" style='display:none;color:#ee9836;'>Thank you for your message. We'll be in contact with you soon.</p>
<form role="form" action="" method="post" class="form panel-body">
{% csrf_token %}
{{ form }}
<button class="btn btn-primary pull-right" type="submit">Submit</button>
</form>
</div>
我的表单视图在模板标签中:
app_tags.py
from django import template
from django.shortcuts import render, redirect
from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context
from .forms import ContactForm
register = template.Library()
# contact form for footer
@register.inclusion_tag('contact_form.html')
def contact(request):
form = ContactForm
if request.method == 'POST':
contact_form = form(data=request.POST)
if contact_form.is_valid():
contact_name = request.POST.get('name', '')
contact_email = request.POST.get('email', '')
contact_message = request.POST.get('message', '')
# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = Context({
'contact_name': contact_name,
'contact_email': contact_email,
'contact_message': contact_message,
})
content = template.render(context)
print content
email = EmailMessage(
"Contact Form",
content,
"contact@site.com" +'',
['email@sample.com'],
headers = {'Reply-To': contact_email }
)
email.send()
else:
contact_form = form()
return {'form': form}
contact_template.txt
Contact Name:
{{ contact_name }}
Email:
{{ contact_email }}
Message:
{{ contact_message }}
这里有一些回溯:
return opener.open(request)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request
尝试使用 reply_to kwarg 而不是在 headers 中传递它。 例如:
email = EmailMessage(
"Contact Form",
content,
"contact@site.com",
['email@sample.com'],
reply_to=[contact_email],
)
https://docs.djangoproject.com/en/1.10/topics/email/#emailmessage-objects