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模板加载静态资源的更多相关文章

  1. 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(). ...

  2. flask模板应用-加载静态文件:添加Favicon,使用CSS框架,使用宏加载静态资源

    加载静态文件 一个Web项目不仅需要HTML模板,还需要许多静态文件,比如CSS.JavaScript文件.图片和声音声.在flask程序中,默认需要将静态文件存储在与主脚本(包含程序实例的脚本)同级 ...

  3. Thymeleaf模板引擎绕过浏览器缓存加载静态资源js,css文件

    浏览器会缓存相同文件名的css样式表或者javascript文件.这给我们调试带来了障碍,好多时候修改的代码不能在浏览器正确显示. 静态常见的加载代码如下: <link rel="st ...

  4. Flask之加载静态资源

    Flask之加载静态资源 1.加载css样式 <link rel="stylesheet" href="{{ url_for('static',filename=' ...

  5. nginx反向代理部署springboot项目报404无法加载静态资源

    问题:nginx反向代理部署springboot项目报404无法加载静态资源(css,js,jpg,png...) springboot默认启动端口为8080,如果需要通过域名(不加端口号)直接访问s ...

  6. 关于springboot2.*版本无法加载静态资源

    前言 在学习springboot的过程中,发现无法引用静态资源.我使用的是springboot2.2.1版本. 追溯源码,终于解决.并记录下解决思路. 默认加载路径 首先得知道springboot默认 ...

  7. 页面性能优化:preload预加载静态资源

    本文主要介绍preload的使用,以及与prefetch的区别.然后会聊聊浏览器的加载优先级. preload 提供了一种声明式的命令,让浏览器提前加载指定资源(加载后并不执行),在需要执行的时候再执 ...

  8. Django 模板中引用静态资源(js,css等)

    Django 模板中免不了要用到一些js和CSS文件,查了很多网页,被弄得略晕乎,还是官网靠谱,给个链接大家可以自己看英文的. https://docs.djangoproject.com/en/1. ...

  9. Spring 加载静态资源

    <mvc:default-servlet-handler/> JSP 中通过标签加载js文件或者link标签加载css文件等静态资源时要在springmvc的xml文件中配置以上设置请求就 ...

随机推荐

  1. Word中如何让两个图片垂直居中对齐

    如图,两个图片都剧终了,大小也一致,但是发现没有对齐,那是因为需要设置(矩阵框住的)缩进,让两个在一起,上下对齐.

  2. UIViewController的生命周期及iOS程序运行顺序

    当一个视图控制器被创建,并在屏幕上显示的时候. 代码的运行顺序 1. alloc                                   创建对象,分配空间 2.init (initWit ...

  3. Linux用户配置sudo权限(visudo)

    sudo的工作过程如下: 1,当用户执行sudo时,系统会主动寻找/etc/sudoers文件,判断该用户是否有执行sudo的权限 2,确认用户具有可执行sudo的权限后,让用户输入用户自己的密码确认 ...

  4. Git 历险记(三)——创建一个自己的本地仓库

    如果我们要把一个项目加入到Git的版本管理中,可以在项目所在的目录用git init命令建立一个空的本地仓库,然后再用git add命令把它们都加入到Git本地仓库的暂存区(stage or inde ...

  5. http://blog.csdn.net/emoven/article/details/12999265

    http://blog.csdn.net/emoven/article/details/12999265

  6. leetcode 题解:Merge Sorted Array(两个已排序数组归并)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...

  7. ElasticSearch的内存设置

    编辑ElasticSearch中bin目录下 vi elasticsearch中 加上   export ES_HEAP_SIZE=24g 修改配置文件 config/elasticsearch.ya ...

  8. How to check the 'OLE DB Destination' INPUT and OUTPUT

    Step 1: Step 2: Step 3:

  9. 标准库priority_queue的一种实现

    优先级队列相对于普通队列,提供了插队功能,每次最先出队的不是最先入队的元素,而是优先级最高的元素. 它的实现采用了标准库提供的heap算法.该系列算法一共提供了四个函数.使用方式如下: 首先,建立一个 ...

  10. Step 2---有关Github的几个问题

    1.取得项目的Git仓库的方式 第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库. 要对现有的某个项目开始用 Git 管理,只需到此项目所在的目录,执行: $ git init 初始化 ...