日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/6/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?=?(??
  • ????#?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/

    ?

    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--静态文件路径和模板路径配置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。