django 1.10媒体图像未显示 [英] django 1.10 media images don't show

查看:115
本文介绍了django 1.10媒体图像未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在网站urls.py中添加以下内容,我已经在现有的django 1.7项目中使用了django媒体图像:

I have had django media images working in an existing django 1.7 project by adding the following to site urls.py:

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

此url结构在django 1.10中不起作用,因此我将其更改为此处推荐的

This url structure doesn't work in django 1.10 and so I changed it to the reccommended here Django MEDIA_URL and MEDIA_ROOT:

urlpatterns = [

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

此操作无法渲染任何上传的媒体图像.我可以使用django 1.10的等效媒体网址格式吗?

推荐答案

您可以使用以下方法: ( Django docs 1.10在开发过程中为用户上传的文件提供服务)

You can use this: (Django docs 1.10 Serving files uploaded by a user during development)

urlpatterns = [
   ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

或者您可以使用它(如果您只想在设置中使用Debug = True进行开发):

OR you can use this (if you only want it in development with the Debug = True in you'r settings): Django docs 1.10 Serving files in development

if settings.DEBUG:
    urlpatterns += [
        url(r'^media/(?P<path>.*)$', serve, {
            'document_root': settings.MEDIA_ROOT,
        }),
    ]

对我来说, {{MEDIA_URL}} 在我的模板文件中不再起作用,我使用了 {%get_media_prefix%} :

For me the {{ MEDIA_URL }} didn't work anymore in my template file, I used the {% get_media_prefix %}:

例如:

<img src="{% get_media_prefix %}{{ product.image }}">

这篇关于django 1.10媒体图像未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆