我在我的应用程序中使用 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)
嗯,正如您所见,我不允许用户上传图像。选择头像图片的流程是:
- 用户访问配置文件配置页面
- 此页面有一个按钮,可打开带有图像选项的模式,用户必须选择一个。
- 用户选择图像。
- 用户点击保存更改。
我在个人资料的头像字段中保存的是一个必须连接到静态 URL 的字符串,例如,如果图像路径是:
127.0.0.1:8000/static/account_settings/avatar-images/man2.jpeg
我正在保存:
account_settings/avatar-images/man2.jpeg
我已经在生产中完成了这个工作流程,并将 debug 设置为 True(由于错误 500,无法使用 debug = False 进行制作)。因此,我打开了用户公共(public)个人资料页面,它给了我同样的错误。
但我在这个模板中找到了问题的根源:
{% 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'
最佳答案
在看到 Wholevinski 的评论和帮助帮助我找到的详细日志后,我已经弄清楚了问题所在。我不喜欢回答自己的问题,但我会在这里回答,以记录我如何解决问题。
嗯,发生的事情是我正在使用 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,问题就消失了。
关于python - 当 debug 设置为 False 时,生产中出现错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48188133/