|
|
浏览器访问:9 j4 F9 R3 { [& o d( I
http://127.0.0.1:5000/hello/render/str// ^3 ?. m C. R. T/ A
结果:
3 b, [& c( h3 j; M1 K$ W$ ?2 \8 o6 ~TemplateDoesNotExist at /hello/render/str/
4 P( k: h& ]; `# a, Lindex.html
* W$ k; z6 I# E/ W) QRequest Method: GET* c; G, H& A' M r/ v
Request URL: http://127.0.0.1:5000/hello/render/str/5 H1 K" `1 d+ f6 }. F5 j0 n" y
Django Version: 3.0.5
+ `7 J) w J- w# G' M; ZException Type: TemplateDoesNotExist8 T! N% H( ]6 Y! i
Exception Value: - X1 z/ ?; m4 H* l7 a
index.html
! L, o& Z+ i" K$ cException Location: C:\Users\long\AppData\Local\Programs\Python\Python38\lib\site-packages\django\template\loader.py in get_template, line 19
1 V$ e- ? ]) |4 r/ _8 dPython Executable: C:\Users\long\AppData\Local\Programs\Python\Python38\python.exe+ ^2 Y) G+ p2 p( R9 |- g6 R, n% R
Python Version: 3.8.8! F t# h) N6 Y
Python Path:
( i# v7 _0 c( |* Q4 ~6 z['C:\\Users\\long\\Desktop\\my_project',
: K% e6 F: P' Y+ e. q4 s 'C:\\Users\\long\\Desktop\\my_project',
& p4 x; G& e, \ 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',8 ~, F% u7 R9 Z: s
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',5 D- `# z( N% b0 c6 B7 {
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib',
" n* [) S E. C; Y X 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38',; L* M) C% ~- L+ f" X/ i) B
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']! u R8 r" X# b
Server time: Sun, 6 Apr 2025 11:17:52 +0000
* p7 C' A' @0 f% h0 U) L+ D
6 j8 V" _# _3 g2 I" S提示来看,是因为找不到templates的文件。
2 r$ [8 f' X9 G! q% d: C- n2 ` X. \0 q9 T/ ~& e- M
找了很久,也没有找到好的文档进行说明。最后抱着试试的态度,修改下项目目录下的setting.py中:2 |3 @4 w7 i d1 q! ^0 m
TEMPLATES = [3 X* d7 \5 y3 e
{
+ e1 ]) M" P9 |8 q% z 'BACKEND': 'django.template.backends.django.DjangoTemplates',7 ?4 n3 c; \1 {1 J0 p2 {0 Z
'DIRS': ['templates'],6 ?7 d: _! E/ n! ^& u v+ h
'APP_DIRS': True,7 n2 S" x3 G$ g B
'OPTIONS': {# p/ ^- z+ J# }
'context_processors': [
E. d+ U$ q# Z7 A 'django.template.context_processors.debug',
# P; d* E1 ]4 A9 [ 'django.template.context_processors.request',3 I; K, K7 K( S# w' ?) n
'django.contrib.auth.context_processors.auth',6 W6 E7 I% c4 G: _) T5 @
'django.contrib.messages.context_processors.messages',
/ q/ |, U' r) J# T$ T( r ],4 ?# t/ n, z: h
},( R$ D R; P9 ~( N, m
},* X) r# F; q& H
]0 U7 y8 W* o; s/ |5 ?6 T7 L" A
改成我们项目的一些配置:如下修改:5 n6 n' j4 M& w6 n# Z( w3 ~$ |
TEMPLATES = [" Y- K7 Y; R$ d% F3 C3 b
{3 x" r9 v0 P' x/ r
'BACKEND': 'django.template.backends.django.DjangoTemplates',
' Z0 y9 @: g. s; ? 'DIRS': ['my_project/templates'],
3 n0 \# `3 }/ l. p 'APP_DIRS': True,) L% V A1 {/ M; r) T( d
'OPTIONS': {- p, B: o, \6 l2 Z+ P
'context_processors': [5 ~' P* f1 b7 E
'django.template.context_processors.debug',! W/ p5 |* ?' a
'django.template.context_processors.request',5 v# z9 |$ O" _3 E6 |' S2 [
'django.contrib.auth.context_processors.auth',8 w+ L7 J# w3 V+ X1 _/ O* U0 k
'django.contrib.messages.context_processors.messages',4 ?8 Y' s3 ]/ s7 J1 b: y9 I
],
; L6 i, S l8 C k },+ M3 u9 h0 n' `
},( r4 S" C! J* m Y8 ]: l
]
2 B+ G" t+ M( j! P3 J再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000" L! C% |0 R) t P, {0 @
再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签# e+ Z: u# e3 l5 j
: K+ k, b: O Y7 E* s问题解决。
7 R# a5 U) v) F7 ~; s/ ^0 b- [( V J k) B0 ]8 V# ^) j7 c
( i2 B: d9 E% ^9 J$ ?% [* v
|
|