2012-12-29 58 views
-2

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

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/ 

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

回答

0

你可以做到以下几点:

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

-1

更新阅读urls.py

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

相关问题