有时候,一点小问题,就会纠结你半天,真是基础不牢啊。 彻底解决 django的模板templates路径的问题; 一般会提示什么模板找不到,类似:TemplateDoesNotExist at /ac

有时候,一点小问题,就会纠结你半天,真是基础不牢啊。 彻底解决 django的模板templates路径的问题; 一般会提示什么模板找不到,类似:TemplateDoesNotExist at /accounts/login/ registration/login.html

一个提示 SyntaxError: EOL while scanning string literal :反斜杆错误 \ 不是 \

我的mysite工程的整个目录:

C:\web\mysite>tree /F
卷 C 的文件夹 PATH 列表
卷序列号为 00000200 0007:9B9B
C:.
│  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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

templates目录一般默认在app下面,我的例子中,templates就在应用blog下面,如果有什么html,会自动识别的,如果你的templates目录是这么建立的,那就不用再mysite/mysite/settings.py中说明templates目录在哪,django会自动查找的,常在APP各自的templates目录中中保存APP特定的模板,并不直接在APP对应templates目录中直接存放模板文件本身,而是在该目录中在创建一层以APP名称命名的目录:mystite/bolg/templates/blog

但是,我非得不走寻常路,我要把templates建立在某个地方,怎么办。

我在写用户认证的是时候需要一个模板login.html,其规定默认的目录是:mysite/mysite/templates/login.html,第二个mysite目录又不是app,你的templates是不会被识别的吧,怎么办,我们在mysite/mysite/settings.py的文件中自定义下我这个templates目录在哪,代码:

TEMPLATE_DIRS = ( 
    os.path.join(BASE_DIR, "mysite\\templates").replace('\\','/'),#注意逗号
   )
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

什么意思,在settings.py中规定了,

import os

BASE_DIR = os.path.dirname(os.path.dirname(file))

base_dir 就是工程project的目录,工程目录下一级就是应用app的目录。

os.path.dirname(file):settings.py文件所在的目录,我的是:C:\web\mysite\mysite

os.path.dirname(os.path.dirname(file)) :又来一次,就是 C:\web\mysite\mysite的上一级目录,结果是:C:\web\mysite,也就是说 BASE_DIR是 C:\web\mysite 也就是工程的目录。

下面解释下:

os.path.join(BASE_DIR, "mysite\\templates").replace('\\','/'),#\\转义\
 
 
  • 1
  • 1

我们替换掉base_dir

os.path.join(BASE_DIR, “mysite\templates”).replace(‘\’,’/’),

os.path.join(“C:\web\mysite”, “mysite\templates”).replace(‘\’,’/’),

我们不看.replace(‘\’,’/’),

结果是: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',
   )
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值