我一直在開發自己的Django的網站和所遇到的問題,Django的 - 圖片上傳保存到正確的位置,但無法顯示
我有一個模型:
class Text_Image_Page(TrainingModulePage):
page_num = models.IntegerField(default = 0)
title = models.CharField(max_length=200)
text = models.CharField(max_length=200)
image_desc = models.CharField(max_length=200)
image = models.ImageField(upload_to=settings.MEDIA_ROOT, default = 'null')
training_module = models.ForeignKey(TrainingModule, related_name='text_image_pages', null=True)
def __str__(self):
return "Image Page"
我使用modelForm來創建這個模型,並且一切正常,直到我嘗試在模板中顯示模型。
我的媒體的根在我的settings.py是
STATIC_URL = '/static/'
MEDIA_URL = '/superchem_media/'
STATIC_ROOT = '/home/anthonycalab/webapps/superchem_static/'
MEDIA_ROOT = '/home/anthonycalab/webapps/superchem_media/'
的圖像是否被正確上傳到媒體的根,因爲我可以看到他們通過FileZilla的但是當我去我的模板,以顯示我的形象存在:
<img src={{t.image.url}} alt="{{t.image_desc}}" style="width:304px;height:228px;">
如果我加載圖像直接的聯繫,但不顯示圖像,futhermore我得到一個404錯誤,消息如下:
Request Method: GET
Request URL: http://anthonycalab.webfactional.com/home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg
Using the URLconf defined in superchem.urls, Django tried these URL patterns, in this order:
^training_modules/
^admin/
^accounts/
^superchem\_media\/(?P<path>.*)$
^static\/(?P<path>.*)$
The current URL, home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg, didn't match any of these.
任何想法是我可以做的不正確?
編輯:
的問題已被縮小到:
{{t.image.url}} = /home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg
當我想
/media/[imagelink].jpg
你能顯示你的url設置嗎?看起來像你不設置媒體網址http://stackoverflow.com/questions/5517950/django-media-url-and-media-root – AlmasK89
'urlpatterns + =靜態(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT) urlpatterns的+ = staticfiles_urlpatterns(settings.STATIC_URL)' 的問題是: image.url是給我: /home/anthonycalab/webapps/superchem_media/image.jpg 代替: media/image.jpg –