【问题标题】:Error 500 in production just when debug is set to False仅当调试设置为 False 时,生产中出现错误 500
【发布时间】:2018-06-19 15:58:06
【问题描述】:

我在我的应用程序中使用 Django 1.11。

我已经使用 django-registration 实现了身份验证,并创建了一个配置文件模型来存储一些用户信息:

class Profile(models.Model):
    ...
    user = models.OneToOneField(User)
    nick = models.CharField(max_length=50)
    level = models.PositiveIntegerField(null=True)
    avatar = models.CharField(max_length=500, null=True, blank=True)
    ...

正在使用信号创建/保存此模型:

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

post_save.connect(create_user_profile, sender=User)
post_save.connect(save_user_profile, sender=User)

如您所见,我不允许用户上传图片。选择头像图片的工作流程是:

  1. 用户访问配置文件配置页面
  2. 此页面有一个按钮,用于打开带有图像选项的模式,用户必须选择一个。
  3. 用户选择一张图片。
  4. 用户点击保存更改。

我在配置文件的头像字段中保存的是一个必须连接到静态 url 的字符串,例如,如果图像路径是:

127.0.0.1:8000/static/account_settings/avatar-images/man2.jpeg

我正在保存:

account_settings/avatar-images/man2.jpeg

我在生产中通过这个工作流程,调试设置为 True(由于错误 500,无法使用 debug = False)。所以,我打开了用户公开个人资料页面,它给了我同样的错误。

但是我在这个模板中找到了问题的根源:

{% if public_user.profile.avatar %}
    <img class="img-fluid w-100 u-block-hover__main--zoom-v1" src="{% static public_user.profile.avatar %}" alt="User Avatar Image">
{% else %}
    <img class="img-fluid w-100 u-block-hover__main--zoom-v1" src="{% static 'assets/img/tmp/avatar.jpg' %}" alt="User Avatar Image">
{% endif %}

如果 public_user.profile.avatar 中存在某些内容,我会收到错误 500。但如果配置文件没有图像,它可以正常工作。我不知道为什么这段代码不起作用:

{% static public_user.profile.avatar %}

知道为什么这段代码的结果让我出错了吗?

错误日志:

2018-01-10T13:53:05.153051+00:00 app[web.1]:     url = self.url(context)
2018-01-10T13:53:05.153052+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py", line 102, in url
2018-01-10T13:53:05.153053+00:00 app[web.1]:     return self.handle_simple(path)
2018-01-10T13:53:05.153053+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py", line 117, in handle_simple
2018-01-10T13:53:05.153054+00:00 app[web.1]:     return staticfiles_storage.url(path)
2018-01-10T13:53:05.153055+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 162, in url
2018-01-10T13:53:05.153055+00:00 app[web.1]:     return self._url(self.stored_name, name, force)
2018-01-10T13:53:05.153056+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 141, in _url
2018-01-10T13:53:05.153057+00:00 app[web.1]:     hashed_name = hashed_name_func(*args)
2018-01-10T13:53:05.153057+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 432, in stored_name
2018-01-10T13:53:05.153063+00:00 app[web.1]:     raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
2018-01-10T13:53:05.153064+00:00 app[web.1]: ValueError: Missing staticfiles manifest entry for 'account_settings/avatar-images/man2.674506bb8a45.jpeg'

【问题讨论】:

  • 如果您得到 500,则几乎可以肯定日志中存在错误。也请在此处发布。
  • 出于安全考虑,Django 仅允许您在启用调试时执行某些操作。我会检查您的设置。如果您尝试设置日志记录以查看实际错误,这也可能会有所帮助。
  • @wholevinski 在我使用“heroku 日志”访问的日志中,只有一行包含错误代码和已完成的 GET 请求。我正在尝试找到一种获取详细日志的方法,我会尽快用它更新我的问题
  • 嗯,好的。看起来 Heroku 需要一个流记录器。所以,把它放在你的配置文件中:LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { 'class': 'logging.StreamHandler', }, }, 'loggers': { 'django': { 'handlers': ['console'], 'level': 'DEBUG' } } } 之后,你应该通过运行heroku logs获得更多的日志记录
  • 实际上,请检查您的设置文件。查看您是否将STATICFILES_STORAGE 设置为ManifestStaticFilesStorage。如果是这样,请注释掉该行以查看是否可以修复它。检查此线程:stackoverflow.com/a/34586172/769971

标签: python django django-templates


【解决方案1】:

尝试将管理员电子邮件添加到您的设置中,这将为您提供一种方法来确定发生错误 500 时的错误消息是什么

SERVER_EMAIL = 'ur@from-email-address.com' 管理员 = ( ('例外电子邮件','destination@email.com'), )

您可能还需要设置 smtp 设置

【讨论】:

    【解决方案2】:

    试试这个

    {% if public_user.profile.avatar %}
        {% with public_user.profile.avatar as img_url %}
        <img class="img-fluid w-100 u-block-hover__main--zoom-v1" src="{% static img_url %}" alt="User Avatar Image">
        {% endwith %}
    {% else %}
        <img class="img-fluid w-100 u-block-hover__main--zoom-v1" src="{% static 'assets/img/tmp/avatar.jpg' %}" alt="User Avatar Image">
    {% endif %}
    

    【讨论】:

      【解决方案3】:

      在查看了 Wholevinski 在他的 cmets 以及他的帮助下帮助我找到的详细日志之后,我已经弄清楚了问题所在。我不喜欢回答我自己的问题,但我会在这里回答,以记录我是如何解决问题的。

      嗯,发生的事情是我正在使用 WhiteNoise 来提供我的静态文件,它正在压缩我的静态文件以便能够拥有 cache support

      在我的代码中:

      STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
      

      我的代码中的以下行使 whitenoise 创建文件版本,并将文件内容的 md5 哈希附加到原始文件名,例如,“filename.jpeg”生成一个文件“filename.674506bb8a45.jpeg”,它将提供给用户。如果服务器文件与用户浏览器中缓存的文件不同,则 md5 将不同,新文件将提供给用户。

      问题是我通过 javascript 获取图像文件名并将其保存在我的数据库中:

      account_settings/avatar-images/man2.674506bb8a45.jpeg
      

      而不是

      account_settings/avatar-images/man2.jpeg
      

      我为解决这个问题所做的就是更改我的 javascript 以忽略 md5 版本。现在我正在保存 account_settings/avatar-images/man2.jpeg,问题就消失了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-08
        • 2021-08-13
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 2021-01-02
        • 2015-02-16
        • 2019-11-04
        相关资源
        最近更新 更多