django--静态文件路径和模板路径配置

来源:互联网 发布:淘宝激活windows7密钥 编辑:程序博客网 时间:2024/05/16 11:10

 1:django处理静态文件:

 

比如 : 我的工程是xiaoshuo-----》进入 小说 ---》 manage.py  xiaoshuo  在进入:

在下面建立一个 static 和templates文件夹

 

打开  settings.py :

 

import os

 

Java代码  收藏代码
  1. STATICFILES_DIRS = (  
  2.     # Put strings here, like "/home/html/static" or "C:/www/django/static".  
  3.     # Always use forward slashes, even on Windows.  
  4.     # Don't forget to use absolute paths, not relative paths.  
  5.     os.path.join( os.path.dirname(__file__),'static').replace('\\','/'),  
  6. )  
 

 

 

在后面加上路径,django1.4会自动找到static下的静态文件,不需要配置urls.py了

 

比如:

http://localhost:8000/static/css/home.css

 

2:配置templates路径:

 

 

Python代码  收藏代码
  1. TEMPLATE_DIRS = (  
  2.     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  
  3.     # Always use forward slashes, even on Windows.  
  4.     # Don't forget to use absolute paths, not relative paths.  
  5.     os.path.join( os.path.dirname(__file__),'tempates').replace('\\','/'),  
  6. )  
 

就可以了.....

 

对应模板的应用参考  http://djangobook.py3k.cn/2.0/chapter04/

 

Java代码  收藏代码
  1. from django.shortcuts import render_to_response  
  2.   
  3. def detail(request):  
  4.     return render_to_response('detail.html')  
 

 

建立views.py文件直接返回html页面到浏览器

 

在urls.py中添加:

 ('^detail/$', detail),

 

浏览器中输入:http://localhost:8000/detail/

 

common下base.html内容

 

 

Java代码  收藏代码
  1. <link rel="stylesheet" href="css/style.css" type="text/css">  
  2. <link rel="stylesheet" href="css/reset.css" type="text/css">  
  3. <link rel="stylesheet" href="css/home.css" type="text/css">  
  4. <script type="text/javascript" src="js/jquery-1.7.1.js"></script>  
  5. <script type="text/javascript" src="js/jquery.wookmark.js"></script>  

 

 上级目录下detail.html内容:

 

 

Java代码  收藏代码
  1. <head>  
  2. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  3. <title>Insert title here</title>  
  4. {% include "common/base.html" %}  
  5. </head>  

 和jsp中处理的inlcude相似:注意相对路径 django是相对访问的url路径的。

 

................

 

上面的base.html是改成这样就可以访问css和js了

Java代码  收藏代码
  1. <link rel="stylesheet" href="../static/css/style.css" type="text/css">  
  2. <link rel="stylesheet" href="../static/reset.css" type="text/css">  
  3. <link rel="stylesheet" href="../static/css/home.css" type="text/css">  
  4. <script type="text/javascript" src="../static/js/jquery-1.7.1.js"></script>  
  5. <script type="text/javascript" src="../static/js/jquery.wookmark.js"></script>  
0 0
原创粉丝点击