【问题标题】:django-simple-captcha breaking heroku deploydjango-simple-captcha 打破 heroku 部署
【发布时间】:2017-02-14 11:47:56
【问题描述】:

我正在使用 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'

(注意如果我注释掉这两条验证码,页面会在部署时加载)

【问题讨论】:

  • 旁注,如果有更详细的日志可用,请指出。
  • 您在本地对settings.py 有任何未提交的更改吗?还是您在 Heroku 上使用不同的设置?听起来该应用可能不在您的 INSTALLED_APPS 中。
  • 不,它肯定在INSTALLED_APPS。我应该澄清一下,虽然它在本地工作,但在本地运行测试套件确实会导致三个失败的测试,都出现上述错误。不过似乎对本地没有任何影响。
  • 您将不得不深入研究那个 HTTP 500 错误。你能在 Heroku 上启用调试模式吗?
  • @Chris 我还没有找到任何方法来做到这一点——你是说我刚刚发送了DEBUG=True 并将其推送到生产环境吗?我被引导相信这会导致黑洞或其他东西并毁灭人类。

标签: python django heroku


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2017-06-24
    相关资源
    最近更新 更多