这个问题还整了半天,问了兄弟波仔,终于解决!
先看效果!
代码如下:
[root@localhost templates]# more base.html
{% load staticfiles %}
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<style type="text/css">
#body{color:#efd;background:#000000;padding:0 5em;margin:0}
body{color:#efd;background:url("{% static "images/background.gif" %}");padding:0 5em;margin:0}
#body {background: white url("images/background.gif") no-repeat right bottom;}
h1{padding:2em 1em}
#h1{padding:2em 1em;background:#080000}
h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em}
p{margin:1em 0;color:#080067}
</style>
</head>
<body>
{% load static %}
<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
<a href="http://www.baidu.com"><img src="{{STATIC_URL}}images/sitelogo.png" alt="static_url type" /></a>
<a href="http://www.baidu.com"><img src="/jingtai/images/sitelogo.png" alt="abs type" /></a>
{% block content %}{% endblock %}
</body>
</html>
[root@localhost templates]# pwd
/root/Desktop/data/download/django/mysite16_5_demo4/news/templates
[root@localhost templates]#
显示效果如下
先看第一种显示方式:
<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
使用tag的形式
普通配置即可,如下:
[root@localhost mysite16_5_demo]# pwd
/root/Desktop/data/download/django/mysite16_5_demo4/mysite16_5_demo
[root@localhost mysite16_5_demo]# tail -10 settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
MEDIA_ROOT = '/headImg'
MEDIA_URL = '/data/'
STATIC_ROOT='/54Static'
#STATIC_URL = '/static/'
STATIC_URL='/jingtai/'
[root@localhost mysite16_5_demo]#
html解析如下
再看第三种显示方式,直接写全路径!setting.py配置同上!缺点是不易于维护
主要是第二种显示方式
setting配置还是同上,但是图片总是显示不出来!
显示效果如下:
修改显示的方法
#year
def year_archive(request, year):
a_list = Article.objects.filter(pub_date__year=year)
return render_to_response('year_archive.html', {'year': year, 'article_list': a_list},RequestContext(request))
将
return render_to_response('year_archive.html', {'year': year, 'article_list': a_list})
改为
return render_to_response('year_archive.html', {'year': year, 'article_list': a_list},RequestContext(request))
增加
RequestContext(request)
记得引用
from django.template import RequestContext
---add 2015年8月13日 17:57:45
显示静态文件
urls.py配置如下
[root@localhost mysite16_5_demo]# more urls.py
from django.conf.urls import patterns, include, url
import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite16_5_demo.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
#(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': 'media'}),
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
url(r'^news/', include('news.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^comm/',include('comm.urls')),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT }),
url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}),
)
[root@localhost mysite16_5_demo]#
add 2015年8月14日 01:00:08
如果我写的路径不是static,而是media呢,我该怎么显示
按照之前的思路,我应该这样改就可以了呀
<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
<a href="http://www.baidu.com"><img src="{{MEDIA_URL}}upload/5.jpg" alt="static_url type" /></a>
<a href="http://www.baidu.com"><img src="/data/upload/5.jpg" alt="abs type" /></a>
但是除了第一个,其他的都没有正确显示,难道哪里错了,按照逻辑来说的话应该没变呀
后台打印的是
[12/Aug/2015 23:16:28] "GET /data/upload/5.jpg HTTP/1.1" 404 2533
难道是找不到这个路路径,将settings.py 中的MEDIA_URL='/media/'时
同事修改模版为
<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
<a href="http://www.baidu.com"><img src="{{MEDIA_URL}}upload/5.jpg" alt="static_url type" /></a>
<a href="http://www.baidu.com"><img src="/media/upload/5.jpg" alt="abs type" /></a>
图片正常显示
其实原因为我urls.py的配置
后台打印为
[12/Aug/2015 23:21:36] "GET /news/articles/2014/ HTTP/1.1" 200 14799
[12/Aug/2015 23:21:36] "GET /jingtai/images/sitelogo.png HTTP/1.1" 304 0
[12/Aug/2015 23:21:36] "GET /jingtai/images/background.gif HTTP/1.1" 304 0
[12/Aug/2015 23:21:36] "GET /media/upload/5.jpg HTTP/1.1" 304 0