调试设置为False时,生产中出现错误500 [英] Error 500 in production just when debug is set to False

查看:86
本文介绍了调试设置为False时,生产中出现错误500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Django 1.11 in my application.

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

I've implemented the authentication using django-registration and created a profile model to store some user infos:

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)
    ...

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

This model is being created/saved with signals:

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)

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

Well, I am not allowing users to upload images, as you see. The workflow of choosing an avatar image is:


  1. 用户访问配置文件配置页面

  2. 此页面包含单击打开带有图像选项的模态的按钮,用户必须选择一个。

  3. 用户选择图像。

  4. 用户单击以保存更改。

  1. User access profile configuration page
  2. This page has a button that opens a modal with image options, the user has to choose one.
  3. User select an image.
  4. User click to save changes.

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

What I am saving at profile's avatar field is a string that have to be concatenate to the static url, for instance, if the image path is:

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

我正在保存:

account_settings/avatar-images/man2.jpeg

我正在调试中使用此工作流程设置为True(由于错误500,无法通过debug = False进行设置)。因此,我打开了用户公共资料页面,并给了我同样的错误。

I've being through this workflow in production with debug set to True (impossible to make with debug = False because of the error 500). So, I've opened the user public profile page and it gives me the same error.

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

But I've found the root of the problem in this template:

{% 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。但是,如果配置文件中没有图像,则可以正常工作。我不知道为什么此代码无法正常工作:

If exist something in public_user.profile.avatar, I have the error 500. But if the profile has no image, it works fine. I don't know why this code doesn't work:

{% static public_user.profile.avatar %}

任何想法为什么这段代码的结果使我出错?

Any ideia why the result of this code is getting me error?

错误日志:

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'


推荐答案

我在了解了Wholevinski通过他的评论和他的屁股帮助我找到的详细日志后,找出了问题所在距离。我不想回答我自己的问题,但是我会在这里记录我如何解决问题的记录。

I've figured out what was the problem after seing the detailed logs that wholevinski helped me finding with his comments plus his assistance. I don't like to answer my own questions, but I'll do it here to keep a record of how I solved the problem.

嗯,发生的是我正在使用WhiteNoise来提供我的静态文件,并且正在压缩我的静态文件以使其具有缓存支持

Well, what is happening is that I am using WhiteNoise to serve my static files and it's compressing my static files to be able to have cache support.

在我的代码中,我有:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

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

The following line in my code makes whitenoise create file versions with the md5 hash of the file content appended to the original file name, so for instance, "filename.jpeg" generates a file "filename.674506bb8a45.jpeg" that will be served to the user. If the server file is different from what is being cached in the user browser, so the md5 will be different and the new file will be served to the user.

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

The problem was that I was getting the image filename via javascript and it was saving in my database:

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

而不是

account_settings/avatar-images/man2.jpeg

我做了什么解决的问题是更改我的JavaScript以忽略md5版本。现在,我保存了account_settings / avatar-images / man2.jpeg,问题消失了。

What I did to solve the problem was change my javascript to ignore the md5 version. Now I am saving account_settings/avatar-images/man2.jpeg and the problem was gone.

这篇关于调试设置为False时,生产中出现错误500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆