[英]Images not showing on html template Django python
I have a template which has a source to an image, every time I run my html file the image does not display,I have tried some examples but to no success, I have set the url.py, settings.py and html file. 我有一个带有图片来源的模板,每次运行html文件时,图片都不会显示。我尝试了一些示例,但没有成功,我设置了url.py,settings.py和html文件。 Am new to Django.
是Django的新手。
URL.py URL.py
from django.conf.urls.defaults import *
from rulebase.rulesapp.views import myview
import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^rulebase/', include('rulebase.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^myview/$', myview),
(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
)
Settings file 设定文件
MEDIA_ROOT = '/home/user/dev/Django/rulebase/media/'
MEDIA_URL = 'http://127.0.0.1:8080/media/'
ADMIN_MEDIA_PREFIX = '/media/'
HTML <img src="{{ MEDIA_URL }}logo-on-trans.png" width="448" height="255" />
HTML
<img src="{{ MEDIA_URL }}logo-on-trans.png" width="448" height="255" />
I would recommend having the images served by Django's STATIC_URL
and not MEDIA_URL
. 我建议由Django的
STATIC_URL
而不是MEDIA_URL
。
Have a read of the Django Docs . 阅读Django Docs 。
It's as easy as: 就像这样简单:
STATIC_ROOT = ''
STATIC_URL = '/media/'
STATICFILES_DIRS = ('/home/user/dev/Django/rulebase/media/',)
'^media/...'
(no longer need it) '^media/...'
的网址格式(不再需要) <img src="{{ STATIC_URL }}logo-on-trans.png" width="448" height="255" />
and providing that the file is inside the directory you've stated it should be served with the HTML. <img src="{{ STATIC_URL }}logo-on-trans.png" width="448" height="255" />
并确保该文件位于您指定的目录内与HTML一起投放。 Read the docs though, there's important justification for why. 不过请阅读文档,其中有一个重要的理由。
删除并尝试从主URL文件中索引URL,因此以这种方式使用的URL url('',view = index),两个引号url之间为空,删除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.