django模板加载静态资源
1. 目录结构

/mysite/setting.py部分配置:
# Django settings for mysite project.
import os.path TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
# '/data/python/django/mysite/template'
os.path.join(os.path.dirname(__file__), '../template').replace('\\','/'),
#这个例子使用了神奇的 Python 内部变量 __file__ ,该变量被自动设置为代码所在的 Python 模块文件名。 `` os.path.dirname(__file__)`` 将会获取自身所在的文件,即settings.py 所在的目录,然后由os.path.join 这个方法将这目录与 templates 进行连接。如果在windows下,它会智能地选择正确的后向斜杠”“进行连接,而不是前向斜杠”/”。
) STATIC_PATH= os.path.join(os.path.dirname(__file__), '../media').replace('\\','/')
/mysite/urls.py配置:
from django.conf.urls import patterns, include, url
# from django.conf.urls import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from page1.views import *
from django.conf import *
admin.autodiscover() urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_PATH}), url(r'^index/$',index),
url(r'^monitor/$',monitor),
)
/mysite/views.py
# Create your views here.
# coding utf8
from django.template import Template, Context
# from django.http import HttpResponse
from django.shortcuts import render_to_response def index(request):
# return render_to_response('thanks.html')
return render_to_response('index.html')
/template/index.html
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title> <!-- Bootstrap -->
<link href="../media/css/bootstrap.min.css" rel="stylesheet"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>你好,世界!</h1> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="../media/js/bootstrap.min.js"></script>
</body>
</html>
成功加载资源:

备注:上面的index.html用到了bootstrap,参考地址:http://www.bootcss.com/
django模板加载静态资源的更多相关文章
- Django的urls.py加载静态资源图片,TypeError: view must be a callable or a list/tuple in the case of include().
Django的urls.py加载静态资源图片,TypeError: view must be a callable or a list/tuple in the case of include(). ...
- flask模板应用-加载静态文件:添加Favicon,使用CSS框架,使用宏加载静态资源
加载静态文件 一个Web项目不仅需要HTML模板,还需要许多静态文件,比如CSS.JavaScript文件.图片和声音声.在flask程序中,默认需要将静态文件存储在与主脚本(包含程序实例的脚本)同级 ...
- Thymeleaf模板引擎绕过浏览器缓存加载静态资源js,css文件
浏览器会缓存相同文件名的css样式表或者javascript文件.这给我们调试带来了障碍,好多时候修改的代码不能在浏览器正确显示. 静态常见的加载代码如下: <link rel="st ...
- Flask之加载静态资源
Flask之加载静态资源 1.加载css样式 <link rel="stylesheet" href="{{ url_for('static',filename=' ...
- nginx反向代理部署springboot项目报404无法加载静态资源
问题:nginx反向代理部署springboot项目报404无法加载静态资源(css,js,jpg,png...) springboot默认启动端口为8080,如果需要通过域名(不加端口号)直接访问s ...
- 关于springboot2.*版本无法加载静态资源
前言 在学习springboot的过程中,发现无法引用静态资源.我使用的是springboot2.2.1版本. 追溯源码,终于解决.并记录下解决思路. 默认加载路径 首先得知道springboot默认 ...
- 页面性能优化:preload预加载静态资源
本文主要介绍preload的使用,以及与prefetch的区别.然后会聊聊浏览器的加载优先级. preload 提供了一种声明式的命令,让浏览器提前加载指定资源(加载后并不执行),在需要执行的时候再执 ...
- Django 模板中引用静态资源(js,css等)
Django 模板中免不了要用到一些js和CSS文件,查了很多网页,被弄得略晕乎,还是官网靠谱,给个链接大家可以自己看英文的. https://docs.djangoproject.com/en/1. ...
- Spring 加载静态资源
<mvc:default-servlet-handler/> JSP 中通过标签加载js文件或者link标签加载css文件等静态资源时要在springmvc的xml文件中配置以上设置请求就 ...
随机推荐
- 理解 VMWare的3种网络模型 z
在说到VMware的网络模型之前,先说一下VMware的几个虚拟设备: ■ VMnet0:这是VMware用于虚拟桥接网络下的虚拟交换机: ■ VMnet1:这是VMware用于虚拟Host-Only ...
- Navicat 破解版的安装
因为电脑系统换掉,重装系统,重新配置了一下环境,安装Navicat,现记录一下过程,以便下次查询使用. 我们首先百度搜索一款navicat for mysql然后进行下载. 2 当我们下载完成之后首先 ...
- etcd:从应用场景到实现原理的全方位解读
随着CoreOS和Kubernetes等项目在开源社区日益火热,它们项目中都用到的etcd组件作为一个高可用强一致性的服务发现存储仓库,渐 渐为开发人员所关注.在云计算时代,如何让服务快速透明地接入到 ...
- 2017.8.5 VMware的介绍与安装
1 VMware简介 官网地址:http://www.vmware.com VMware的功能: 是一个虚拟PC的软件,可以在现有的操作系统上虚拟出一个新的硬件环境,相当于模拟出一台新的PC,以此来实 ...
- Binder与interface
在Interface中,asBinder函数涌来将服务类接口类型转换为IBinder类型: 相反的,asInterface函数用来将Ibinder类型转换为服务接口类型
- 倒计时:CountDownLatch(火箭发射前的准备)读书笔记
这是一个非常实用的多线程控制工具类,经典的场景就是 火箭发射,在火箭发射前,为了保证万无一失,往往还要进行各项设备,仪器的检查,只有等待所有的检查完毕后,引擎才能点火, CountDown ...
- OpenSSL Heartbleed "心脏滴血"漏洞简单攻击示例
转自:http://www.lijiejie.com/openssl-heartbleed-attack/ OpenSSL Heartbleed漏洞的公开和流行让许多人兴奋了一把,也让另一些人惊慌了一 ...
- jmap命令(Java Memory Map)的使用
jmap的使用能够參考: 官方文档 http://docs.oracle.com/javase/6/docs/technotes/tools/share/jmap.html 和这篇博客 http:// ...
- 51单片机 | 使用D/A转换器实现三角波发生器
———————————————————————————————————————————— D/A转换器 CS=0.ILE=1时,WR1信号有效时将数据总线上的信号写入8位输入锁存器 XFER=0时,W ...
- Effective JAVA 创建和销毁对象 遇到多参构造器考虑使用构建器
//构建器抽象类,为不同类的实现提供 public interface Builder<T> { public T build(); } /** * @描述: 营养表 */ public ...