gpt4 book ai didi

python - Django/Sendgrid 表单 HTTP 错误 400 : Bad Request

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:36 25 4
gpt4 key购买 nike

当我尝试使用 Sendgrid 发送 Django 表单时,出现了 HTTPError: HTTP Error 400: Bad Request 错误。我已经确认 envvars 中有一个 Sendgrid key 。这是我的代码:

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>

我的表单 View 在模板标签中:

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 而不是在标题中传递它。例如:

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

关于python - Django/Sendgrid 表单 HTTP 错误 400 : Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41027786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com