`
iluoxuan
  • 浏览: 570469 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多

 1:django处理静态文件:

 

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

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

 

打开  settings.py :

 

import os

 

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

 

 

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

 

比如:

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

 

2:配置templates路径:

 

 

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.
    os.path.join( os.path.dirname(__file__),'tempates').replace('\\','/'),
)
 

就可以了.....

 

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

 

from django.shortcuts import render_to_response

def detail(request):
    return render_to_response('detail.html')
 

 

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

 

在urls.py中添加:

 ('^detail/$', detail),

 

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

 

common下base.html内容

 

 

<link rel="stylesheet" href="css/style.css" type="text/css">
<link rel="stylesheet" href="css/reset.css" type="text/css">
<link rel="stylesheet" href="css/home.css" type="text/css">
<script type="text/javascript" src="js/jquery-1.7.1.js"></script>
<script type="text/javascript" src="js/jquery.wookmark.js"></script>

 

 上级目录下detail.html内容:

 

 

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
{% include "common/base.html" %}
</head>

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

 

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

 

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

<link rel="stylesheet" href="../static/css/style.css" type="text/css">
<link rel="stylesheet" href="../static/reset.css" type="text/css">
<link rel="stylesheet" href="../static/css/home.css" type="text/css">
<script type="text/javascript" src="../static/js/jquery-1.7.1.js"></script>
<script type="text/javascript" src="../static/js/jquery.wookmark.js"></script>
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics