2013-09-28 74 views
1

未找到引用我剛纔的問題can only concatenate tuple (not "unicode") to tuple靜態媒體形象在Django

現在,我得到以下錯誤,當我嘗試對媒體文件的URL GET請求

Page not found (404) 
Request Method: GET 
Request URL: http://localhost:8000/media/media/abyss.jpg 

Urls.py

urlpatterns = patterns('', 
         url(r'^admin/', include(admin.site.urls)), 
         url(r'^$', 'blog.views.index'), 
         url(r'^blog/(?P<slug>[\w\-]+)/$', 'blog.views.article'), 
         url(r'^(.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), 
         ) 
+0

你的url.py看起來像什麼? – Mingyu

+2

@AjayKumar請更新問題,而不是在評論中發佈。 –

+0

請查看所有文件信息的 – ajkumar25

回答

0

我有同樣的問題,在這裏我就是這樣做來解決這個問題: 我的項目結構

./django_site 
    app1/ 
    app2/ 
    media/ 
    django_site/ 
    settings.py 
    #your other basic config stuff in here 

在settings.py:

MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media') 
MEDIA_URL = '/media/' 

在urls.py inside sub -directory django_site/

#the main url patterns 
urlpatterns = patterns('', 
#urls 
) 

"""Here's where we're putting the route to serve media from MEDIA_URL. 
I couldn't get this part to work inside the inital declaration of urlpatterns. 
Maybe you'll have better luck """ 

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

然後在模板:

<img src="{{ MEDIA_URL }}{{ image_name }}" height="200" width="400"/> 

我從一個函數基於視圖傳遞image_name。看起來,您必須定義http://somesite.com/media/網址,以便能夠從MEDIA_URL拉回媒體。我沒有在文檔中明確地看到這一點,但是,除非您配置url路由,否則GET請求似乎總是失敗。現在,我從模板中或直接通過網址提取文件。希望這可以幫助!

相關問題