python templates_python templates在哪
展開全部
一個(gè)提e5a48de588b662616964757a686964616f31333363373731示?SyntaxError:?EOL?while?scanning?string?literal :反斜桿錯(cuò)誤 \\ 不是 \
我的mysite工程的整個(gè)目錄:C:\web\mysite>tree?/F卷?C?的文件夾?PATH?列表卷序列號(hào)為?00000200?0007:9B9BC:.│??db.sqlite3│??manage.py│├─blog│??│??admin.py│??│??admin.pyc│??│??forms.py│??│??forms.pyc│??│??models.py│??│??models.pyc│??│??tests.py│??│??urls.py│??│??urls.pyc│??│??views.py│??│??views.pyc│??│??__init__.py│??│??__init__.pyc│??││??├─migrations│??│??????0001_initial.py│??│??????0001_initial.pyc│??│??????__init__.py│??│??????__init__.pyc│??││??└─templates│??????└─blog│??????????????base.html│??????????????post_detail.html│??????????????post_draft_list.html│??????????????post_edit.html│??????????????post_list.html│├─mysite│??│??lujing.py│??│??settings.py│??│??settings.pyc│??│??urls.py│??│??urls.pyc│??│??wsgi.py│??│??wsgi.pyc│??│??__init__.py│??│??__init__.pyc│??││??└─templates│??????├─mysite│??????│??????base.html│??????││??????└─registration│??????????????login.html│└─static????└─css????????????blog.css
templates目錄一般默認(rèn)在app下面,我的例子中,templates就在應(yīng)用blog下面,如果有什么html,會(huì)自動(dòng)識(shí)別的,如果你的templates目錄是這么建立的,那就不用再mysite/mysite/settings.py中說明templates目錄在哪,django會(huì)自動(dòng)查找的,常在APP各自的templates目錄中中保存APP特定的模板,并不直接在APP對應(yīng)templates目錄中直接存放模板文件本身,而是在該目錄中在創(chuàng)建一層以APP名稱命名的目錄:mystite/bolg/templates/blog
但是,我非得不走尋常路,我要把templates建立在某個(gè)地方,怎么辦。
我在寫用戶認(rèn)證的是時(shí)候需要一個(gè)模板login.html,其規(guī)定默認(rèn)的目錄是:mysite/mysite/templates/login.html,第二個(gè)mysite目錄又不是app,你的templates是不會(huì)被識(shí)別的吧,怎么辦,我們在mysite/mysite/settings.py的文件中自定義下我這個(gè)templates目錄在哪,代碼:TEMPLATE_DIRS?=?(
os.path.join(BASE_DIR,?"mysite\\templates").replace('\\','/'),#注意逗號(hào)
)
什么意思,在settings.py中規(guī)定了,import?os
BASE_DIR?=?os.path.dirname(os.path.dirname(__file__))
base_dir 就是工程project的目錄,工程目錄下一級(jí)就是應(yīng)用app的目錄。
os.path.dirname(__file__):settings.py文件所在的目錄,我的是:C:\web\mysite\mysite
os.path.dirname(os.path.dirname(__file__)) :又來一次,就是?C:\web\mysite\mysite的上一級(jí)目錄,結(jié)果是:C:\web\mysite,也就是說?BASE_DIR是?C:\web\mysite 也就是工程的目錄。
下面解釋下:os.path.join(BASE_DIR,?"mysite\\templates").replace('\\','/'),#\\轉(zhuǎn)義\
我們替換掉base_diros.path.join(BASE_DIR,?"mysite\\templates").replace('\\','/'),
os.path.join("C:\web\mysite",?"mysite\\templates").replace('\\','/'),
我們不看.replace('\\','/'),
結(jié)果是:os.path.join("C:\web\mysite", "mysite\\templates")==C:\web\mysite\mysite\templates
注意,這和文字的join不同,所以要寫成mysite\\templates 而不是\\mysite\\templates.
r"C:\web\mysite\mysite\templates".replace('\\','/')=='C:/web/mysite/mysite/templates'
所以前面的那段話就是說:TEMPLATE_DIRS?=?(
r'C:/web/mysite/mysite/templates',
)
總結(jié)
以上是生活随笔為你收集整理的python templates_python templates在哪的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wxpython列表控件listctrl
- 下一篇: python语言与c语言相比在分支结构上