django-simple-captcha打破了heroku部署

时间:2017-02-11 13:50:27

标签: python django heroku

我使用的是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'

(请注意,如果我注释掉两个验证码行,则会在部署中加载页面)

1 个答案:

答案 0 :(得分:1)

问题是没有在Heroku上运行数据库迁移。 heroku run python manage.py makemigrationsheroku run python manage.py migrate修正了它。