我正在使用django-simple-captcha,它在本地工作正常。 部署到 Heroku 后,它所在的页面会出现 500 错误(我得到的唯一日志输出是 ( 2017-02-11T13:26:07.367450+00:00 heroku[router]: at=info method=GET path="/contact/" host=fathomless-harbor-1234.herokuapp.com request_id=37555c3c-c468-40cb-a142-c7dd04519e2c fwd="73.163.191.194" dyno=web.1 connect=1ms service=83ms status=500 bytes=386
(。
我运行了./manage.py test captcha
并得到了三个失败的测试,所有这些测试都有类似的错误File "/Users/pmn/.virtualenvs/within/lib/python2.7/site-packages/django/template/loader.py", line 43, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
TemplateDoesNotExist: captcha_test/image.html
我在 django 1.9.6 和 django-simple-captcha 0.5.3 上
forms.py
from django import forms
from django.template.loader import get_template
from django.core.mail import EmailMultiAlternatives
from captcha.fields import CaptchaField
class ContactForm(forms.Form):
contact_name = forms.CharField()
contact_email = forms.EmailField()
contact_phone = forms.CharField()
content = forms.CharField(widget=forms.Textarea)
cc_me = forms.BooleanField(required=False, initial=False)
captcha = CaptchaField(required=True)
def send_email(self):
contact_name = self.data["contact_name"]
contact_phone = self.data["contact_phone"]
contact_email = self.data["contact_email"]
content = self.data["content"]
template = get_template("contact.txt")
context = {
"contact_name": contact_name,
"contact_phone": contact_phone,
"contact_email": contact_email,
"content": content,
}
content = template.render(context)
subject, from_email, to = "Inquiry", contact_email, "jason@email.com"
cc_address = contact_email if "cc_me" in self.data else None
email = EmailMultiAlternatives(
subject,
content,
from_email,
["jason@email.com"],
cc=[cc_address],
headers={"Reply-To": contact_email}
)
email.send()
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form-control'
(请注意,如果我注释掉两个验证码行,页面将在部署中加载(
问题是数据库迁移没有在 Heroku 上运行。 heroku run python manage.py makemigrations
,heroku run python manage.py migrate
修复了它。