我在开发服务器上设置了以下网址格式。 DEBUG = True。我的媒体目录上的图像链接不起作用(即localhost / media / images / img1.jpg不加载图像)。但是,如果我将媒体网址模式插入到评论MAIN URL PATTERNS的部分前面,它可以正常工作,这表明我的媒体链接设置正确。这里发生了什么?
urlpatterns = patterns("",
# works if I insert the media url pattern here
# MAIN URL PATTERNS
(r"^admin/" , include(admin.site.urls)),
(r"^group/(?P<dpk>\d+)/(?P<show>\S+)/" , GroupView.as_view(), {}, "group"),
(r"^group/(?P<dpk>\d+)/" , GroupView.as_view(), {}, "group"),
(r"^add-images/(?P<dpk>\d+)/" , AddImages.as_view(), {}, "add_images"),
(r"^slideshow/(?P<dpk>\d+)/" , SlideshowView.as_view(), {}, "slideshow"),
(r"^image/(?P<mfpk>\d+)/" , ImageView.as_view(), {}, "image"),
(r"^image/" , ImageView.as_view(), {}, "image"),
(r"" , Main.as_view(), {}, "photo"),
# END OF MAIN URL PATTERNS
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT,}),
)
答案 0 :(得分:3)
执行此操作的正确方法是:
if settings.DEBUG:
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + urlpatterns
(这也删除了硬编码的MEDIA_URL)
答案 1 :(得分:0)
您的photo
视图正在捕获所有内容,因为您没有在模式上放置任何开始/结束修饰符。最后两种模式应该是:
(r"^image/$", ...
(r"^$", ...