[英]How to display images in Django 1.10
i am not good in django, i use Django 1.10 and now i have problem with image displaying. 我在django中不太好,我使用Django 1.10,现在图像显示出现问题。 I read in this version some stuff has changed but i don't get it.
我在此版本中阅读了一些内容已更改,但我不明白。 Here is what i have now:
这是我现在所拥有的:
settings.py: settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_DIR = os.path.join(BASE_DIR, 'static')
TEMPLATE_DIR = os.path.join(BASE_DIR, '/templates')
STATICFILES_DIRS = [STATIC_DIR, ]
STATIC_URL = '/static/'
MEDIA_ROOT = 'C:/Users/john/myprojects/goal/website/goal/media/'
MEDIA_URL = '/media/'
Maybe something is unusual here and i can delete it? 也许这里有些不寻常,我可以删除它吗?
template: 模板:
{% block content%}
{{ car.name }}
{{ car.photo.url }}
{% endblock %}
Here is folder where i have my images: 这是我存放图像的文件夹:
C:/Users/john/myprojects/goal/website/goal/media/images
C:/用户/ JOHN / MyProjects下/目标/网站/目标/媒体/图片
And what i see instead of image on my website: 我在网站上看到的而不是图片:
/media/images/mynewcar.jpg
/media/images/mynewcar.jpg
What must i change? 我必须改变什么?
i pasted to urls.py: 我粘贴到urls.py:
media_dir = os.path.join(os.path.dirname( file ),'media')
media_dir = os.path.join(os.path.dirname( file ),'media')
and to urlpatterns 和urlpatterns
url(r'^media/(.*)$','django.views.static.serve',{'document_root': media_dir}),
url(r'^ media /(.*)$','django.views.static.serve',{'document_root':media_dir}),
You have to put <img>
tag 您必须放置
<img>
标签
{% block content%}
{{ car.name }}
<img src="{{ car.photo.url }}">
{% endblock %}
Update 更新
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.