【问题标题】:upload_to property in imageField can not save image on diskimageField 中的 upload_to 属性无法将图像保存在磁盘上
【发布时间】:2012-12-29 08:29:28
【问题描述】:

我是 django 的初学者。我想要一个 imageField 我的 ModelForm 可以保存上传到我磁盘中的 upload_to 路径的图像,但是当我通过管理站点时,我看到该图像保存在一个 url 而不是我磁盘上的目录中。

user_image = models.ImageField( upload_to= '/images/', null=True , blank=True )

setting.py

MEDIA_ROOT = 'C:\...\static'

当我点击管理站点中的图片链接时,它会转到这样的网址:

http://127.0.0.1:8000/admin/time_save/userprofile/6/16.png/

我搜索了很多问题,但没有找到明确的解决方案。 提前致谢,

【问题讨论】:

    标签: django save modelform imagefield


    【解决方案1】:

    您可以执行以下操作:

    UPLOAD_ROOT = 'C:\...\static'
    
    upload_storage = FileSystemStorage(location=UPLOAD_ROOT, base_url='/uploads')
    
    image = models.ImageField(upload_to='/images', storage=upload_storage)
    

    PS:您可以在设置文件中定义 UPLOAD_ROOT。

    还请仔细阅读设置您自己的custom storage

    【讨论】:

      【解决方案2】:

      更新urls.py

      from django.conf.urls.static import static urlpatterns = [ url(....),
      ] if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

      【讨论】:

        猜你喜欢
        • 2012-06-23
        • 1970-01-01
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        • 2011-06-27
        • 1970-01-01
        • 2011-10-31
        相关资源
        最近更新 更多