生活随笔
收集整理的這篇文章主要介紹了
django--静态文件路径和模板路径配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?1:django處理靜態文件:
?
比如 : 我的工程是xiaoshuo-----》進入 小說 ---》 manage.py ?xiaoshuo ?在進入:
在下面建立一個 static 和templates文件夾
?
打開 ?settings.py :
?
import os
?
Java代碼??
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路徑:
?
?
Python代碼??
TEMPLATE_DIRS?=?(?? ?????? ?????? ?????? ????os.path.join(?os.path.dirname(__file__),'tempates').replace('\\','/'),?? )??
?
就可以了.....
?
對應模板的應用參考 ?http://djangobook.py3k.cn/2.0/chapter04/
?
Java代碼??
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內容
?
?
Java代碼??
<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內容:
?
?
Java代碼??
<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了
Java代碼??
<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> ?
總結
以上是生活随笔為你收集整理的django--静态文件路径和模板路径配置的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。