【问题标题】:django 'InMemoryUploadedFile' object has no attribute 'field'django 'InMemoryUploadedFile' 对象没有属性 'field'
【发布时间】:2011-11-10 06:10:29
【问题描述】:

我在生产服务器上上传文件时遇到问题。我在模型上使用 django-stdimage 如下:

class League(models.Model):
    name = models.CharField(max_length=100)
    logo = StdImageField(upload_to='images/league_logos', blank=True, size=(220, 120))

使用管理应用程序,我可以在本地机器上上传模板(使用开发服务器)就好了。在生产中,我有 nginx 用于静态文件检索,而 apache 和 mod_wsgi 用于其余部分。尝试上传生产时,我得到了

'InMemoryUploadedFile' 对象没有属性'field'

错误。我在 apache 中跟踪错误日志,当我尝试上传时似乎没有发生任何事情。错误发生在

site-packages/django/contrib/admin/templates/admin/includes/fieldset.html,第 19 行错误

这只是一个访问 field.field 的模板标签:

{{ field.field }}

我不确定如何进行调试。有什么建议吗?

谢谢

【问题讨论】:

    标签: django apache file-upload nginx mod-wsgi


    【解决方案1】:

    我发现这是由于我在 pip 安装我的要求时没有安装 PIL。这是一个非常令人沮丧的问题,因为错误消息并没有真正让我明白这一点。

    幸运的是,我遇到了这个帖子:

    Why can't I upload jpg files to my Django app via admin/?

    这让我明白了。

    【讨论】:

      【解决方案2】:

      您可能希望将文件系统存储对象传递给它 - 然后它知道如何将从浏览器获取的 InMemoryUploadedFile 对象转换为 ImageFieldFile,以便保存图像。

      # Near top of your file:
      from django.core.files.storage import FileSystemStorage
      
      fs = FileSystemStorage(location=settings.MEDIA_ROOT)
      
      # Then in your model:
          logo = StdImageField(storage = fs, upload_to='images/league_logos', blank=True, size=(220, 120))
      

      这应该将上传的文件保存到 MEDIA_ROOT/images/league_logos。

      【讨论】:

        猜你喜欢
        • 2014-08-25
        • 1970-01-01
        • 1970-01-01
        • 2014-12-28
        • 2019-10-08
        • 2018-12-16
        • 2020-02-11
        • 2015-02-19
        • 2016-03-30
        相关资源
        最近更新 更多