2016-05-12 37 views
0

我一直在開發自己的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

+0

你能顯示你的url設置嗎?看起來像你不設置媒體網址http://stackoverflow.com/questions/5517950/django-media-url-and-media-root – AlmasK89

+0

'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 –

回答

0

添加媒體的URL在主urls.py文件

from django.contrib import admin 
from django.conf.urls import patterns, include, url 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
from django.conf import settings 


admin.autodiscover() 


urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^', include('home.urls')), 

] 

urlpatterns += patterns('', 
         url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': False}), 
         url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}), 
         ) 
urlpatterns += staticfiles_urlpatterns() 
+0

這仍然給我同樣的問題。 {{img.url}} = /home/anthonycalab/webapps/superchem_media/img.jpg 它應該只是給我/media/img.jpg –

+0

有沒有辦法只返回圖像名稱? 例如image1.jpg –

+0

您是否在您的設置中添加了此代碼。 TEMPLATE_CONTEXT_PROCESSORS =( '-------------------' 'django.core.context_processors.media', 'django.core.context_processors。靜態', '-----------' ) – 2016-05-12 10:19:41

0
settings.py 

import os 

def root(x): 
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',x) 

MEDIA_ROOT = root('media') 
MEDIA_URL = '/media/' 
STATIC_ROOT = root('staticstorage') 
STATIC_URL = '/static/' 

STATICFILES_DIRS = (
    root('static'), 
) 


TEMPLATE_CONTEXT_PROCESSORS = (
    '-------------------' 
    'django.core.context_processors.media', 
    'django.core.context_processors.static', 
    '-----------' 
) 

urls.py 

from django.conf.urls import patterns, include, url 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
from django.conf import settings 

urlpatterns += patterns('', 
         url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': False}), 
         url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}), 
         ) 
urlpatterns += staticfiles_urlpatterns()