首页 > 代码库 > django模板加载静态资源

django模板加载静态资源

1. 目录结构

 

/mysite/setting.py部分配置:

# Django settings for mysite project.import os.pathTEMPLATE_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(\\,/),)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 adminfrom 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 utf8from django.template import Template, Context# from django.http import HttpResponsefrom django.shortcuts import render_to_responsedef 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模板加载静态资源