2013-04-15 88 views
0

我想通過Django管理站點上傳圖像。但它不工作,我已經安裝了PIL包。但它顯示:<django.core.files.storage.FileSystemStorage object at 0x8a92f0c>/mcDonalds.jpgDjango圖像上傳不起作用

from django.core.files.storage import FileSystemStorage 
projectDirPath = path.dirname(path.dirname(__file__)) 
storeImageDir = FileSystemStorage(location=projectDirPath + '/couponRestApiApp/static') 
storeImage = models.ImageField(upload_to=storeImageDir) 

和靜態dir是empty..is我做錯了什麼......

+1

首先,'upload_to'應爲dir的圖像的任何圖像直接設置爲上傳路徑或可調用(請參閱[文檔](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField.upload_to))。請參閱:http://stackoverflow.com/questions/5135556/dynamic-file-path-in-django。 – alecxe

回答

1

試試這個:storeImage = models.ImageField(upload_to="images")

這將追加文件名前: abc.png到圖像/ abc.png然後設置媒體目錄要存儲圖像爲前:yourAppDIR/static/images/....

,並設置URL:

url(r'^tosomeThing/(?P<path>.*)/$', 'django.views.static.serve', 
    {'document_root': MEDIA_URL, 'show_indexes': False}), 

集媒體URL中設置file..to

MEDIA_URL = projectDirPath + '/couponRestApiApp/static/' 

時,你會上傳它會被存儲在您的應用程序