有时候,一点小问题,就会纠结你半天,真是基础不牢啊。 彻底解决 django的模板templates路径的问题; 一般会提示什么模板找不到,类似:TemplateDoesNotExist at /ac
2017-06-02 11:14
951 查看
有时候,一点小问题,就会纠结你半天,真是基础不牢啊。 彻底解决 django的模板templates路径的问题; 一般会提示什么模板找不到,类似:TemplateDoesNotExist at /accounts/login/ registration/login.html
一个提示 SyntaxError: EOL while scanning string literal :反斜杆错误 \ 不是 \
我的mysite工程的整个目录:
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目录在哪,代码:
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 也就是工程的目录。
下面解释下:
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’
所以前面的那段话就是说:
一个提示 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.css1
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', )
相关文章推荐
- django找不到模板(TemplateDoesNotExist at)的异常处理案例
- 解决Django换用jinja2模板引擎后admin提示“TemplateDoesNotExist”
- Django出错提示TemplateDoesNotExist at /
- 解决CentOS 7.2 1611 提示的 “Warning: /dev/root does not exist问题
- Django中提示TemplateDoesNotExist?
- Django 找不到模版报错" django.template.exceptions.TemplateDoesNotExist: index.html"
- django TemplateDoesNotExist at /
- 编译hadoop-0.20.2源码时执行ant eclipse-files出现.eclipse.templates does not exist问题解决方案
- django:TemplateDoesNotExist错误的解决方法
- Django下TemplateDoesNotExist 异常的解决方法:
- python3 Django TemplateDoesNotExist at /
- Django1.10 TemplateDoesNotExist at /index/
- 关于在CI中引用路径的问题 File does not exist
- 关于Service 'XXXX' of type 'MapServer' does not exist or is inaccessible问题的解决
- AIR SDK 0.0: AIR SDK location “...\devsdks\AIRSDK\Win” does not exist.”问题解决~
- 关于编写WPF UserControl时提示The name 'InitializeComponent' does not exist in the current contextr的解决!
- ORA-27101: shared memory realm does not exist 问题解决办法
- 解决ORA-01034: ORACLE not available ORA-27101: shared memory realm does not exist的问题
- Bootstrap: command Tomcat 5.5\conf\server.xml" does not exist. eclipe启动tomcat问题解决方法
- WTP中Publishing failed:Resource /xxxx does not exist问题的解决