django 显示静态文件的几种方式
来源:互联网 发布:每周eia数据时间 编辑:程序博客网 时间:2024/06/07 10:03
这个问题还整了半天,问了兄弟波仔,终于解决!
先看效果!
代码如下:
[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配置还是同上,但是图片总是显示不出来!
显示效果如下:
修改显示的方法
#yeardef 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.pyfrom django.conf.urls import patterns, include, urlimport settingsfrom django.contrib import adminadmin.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
- django 显示静态文件的几种方式
- django 表单提交的几种方式
- django 下载文件的几种方法
- 静态参数传递的几种方式
- Django的静态文件设置
- django--静态文件的配置
- django上的静态文件
- django静态文件的访问
- django中view配置的几种方式
- Django防止XSS攻击的几种方式
- 几种图像显示的编码方式
- NSView显示颜色的几种方式
- Flex显示图片的几种方式
- html 图片显示的几种方式
- input 显示提示的几种方式。
- 立体显示的原理-几种显示方式
- 剖析生成shtml静态页面的几种方式
- 剖析生成shtml静态页面的几种方式
- MAVEN项目模块化
- LeetCode解题报告--Container With Most Water
- Laravel SoapClient
- Spring MVC 之@RequestBody,@ResponseBody详解
- redis常用命令
- django 显示静态文件的几种方式
- 直接插入排序
- win7无法访问虚拟机中的hadoop2.x的web管理界面
- ubuntu14.10配置静态IP中所遇到的DNS问题
- 黑马程序员--Java基础--多线程(二)
- ios中json解析出现的null问题
- 斯坦福ML公开课笔记10——VC维、模型选择、特征选择
- css3 pointer-events: none;
- Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象