|
|
浏览器访问:9 P! N' ?2 V; ~" {, h, Q' O
http://127.0.0.1:5000/hello/render/str/. Y# z' j( E$ Y2 G& S* D
结果:8 b0 }. Y: U5 n$ u4 C; y# G. J
TemplateDoesNotExist at /hello/render/str/
* _) f" K# {% W" Qindex.html" _2 { w. u8 i+ p
Request Method: GET
+ c0 s; t* m$ Q9 P* X# T" C- T- hRequest URL: http://127.0.0.1:5000/hello/render/str/
f& p' Q5 X% N, @9 f m: d# zDjango Version: 3.0.5( A9 Y5 O, U3 W4 H- `: f9 k
Exception Type: TemplateDoesNotExist( u" y) m. K* C" r6 [9 e
Exception Value: T2 o" j" o- E$ m' F
index.html+ Q7 n0 Z# ]8 V5 C( [. |' j9 _$ }
Exception Location: C:\Users\long\AppData\Local\Programs\Python\Python38\lib\site-packages\django\template\loader.py in get_template, line 19
' @) m, y& R8 vPython Executable: C:\Users\long\AppData\Local\Programs\Python\Python38\python.exe
) ^/ i, N" g+ j' APython Version: 3.8.8
* p6 @8 V) c1 J3 Q. K7 `: T+ ?Python Path: ! {# D- f$ S, s8 m% V2 P
['C:\\Users\\long\\Desktop\\my_project',* x) M8 q% w/ A) \0 N# s) n- w$ W
'C:\\Users\\long\\Desktop\\my_project',1 b: x! _- w( E/ Q0 {% Y: t8 ?) P
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',
. {. V* b. u% x) v' s/ S, K4 i 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',5 X- ~0 Z5 q4 r
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib',- m) u( u# r* o" g; i
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38',2 ?0 M' f# X( [& _. ^+ z$ N
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']6 `7 {2 n# B% T7 k& Y3 N* m/ T1 g
Server time: Sun, 6 Apr 2025 11:17:52 +0000- s6 i% a1 M' f- N4 D
% y% }6 C/ r+ V3 W6 z" G1 ^提示来看,是因为找不到templates的文件。& I6 _, g) R( g* E% N
, R. i6 P- p# c
找了很久,也没有找到好的文档进行说明。最后抱着试试的态度,修改下项目目录下的setting.py中:' T: M, `2 A+ J8 V: F
TEMPLATES = [
, y' X4 _- O( v& ^9 [, q" f% s {5 W, ]4 P+ t5 R* Y5 Y6 Q
'BACKEND': 'django.template.backends.django.DjangoTemplates',* P$ ~( p* `0 E+ T3 i' Y+ M2 Q K
'DIRS': ['templates'],
3 f4 d. J% U! L2 _8 C8 \ 'APP_DIRS': True,. S' Q! I, m& a2 B( b2 \$ X" Q. K
'OPTIONS': {) r4 V1 m' V) O% Q! U6 [
'context_processors': [
7 w: ?. w6 G# w 'django.template.context_processors.debug'," r" V- ?: [8 i5 D* J7 Z4 C. h. y1 H
'django.template.context_processors.request',
, ?8 i6 Z- g5 k: \9 ` 'django.contrib.auth.context_processors.auth',
' C- L; k" t! D) { 'django.contrib.messages.context_processors.messages',0 p) g. y, @. \3 N ^ q" Z5 t
],
. U$ ~* E. C7 o, ^ u2 u3 L0 S },
8 z. b7 [ b( o; U; h+ y },) s- ]2 w& o& b7 ~" K; C
]
8 e' U! Y- u- X2 g( c6 g, f改成我们项目的一些配置:如下修改:8 j" z+ q) S6 p
TEMPLATES = [
0 C: R+ G: P0 g/ M' V {; h2 A6 l7 _* C) u* J7 F3 c6 h. J2 W
'BACKEND': 'django.template.backends.django.DjangoTemplates',
0 |& y! v; o0 k1 [+ c% z; t7 j 'DIRS': ['my_project/templates'],% G8 ?% o/ i6 r' [8 Q
'APP_DIRS': True," B" X/ E% [2 x$ J3 T+ p( N
'OPTIONS': {
0 [- C( `% i- ]- `$ b. { 'context_processors': [4 s5 | J& v) G( V
'django.template.context_processors.debug',/ o7 {5 t3 f, P2 x( M
'django.template.context_processors.request',5 t1 z. ?5 l" V2 P) a b+ s
'django.contrib.auth.context_processors.auth',
$ J1 i6 x# I4 j: @7 h 'django.contrib.messages.context_processors.messages',
5 B3 T% |% m8 M* l ],
) n$ Y8 y8 d6 ?7 D: ? },
% N# \' _ N* o q* @/ j/ H },
% d( K: H& H0 K9 `% x]9 w- T& @8 |+ u/ c
再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000
! P8 r! Q) _7 M! F% Y( e! ?再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签
. G+ ^* b' W; K! P' B" U! ]4 B" B0 e2 X, {6 K3 X
问题解决。6 M8 S J0 Z1 K6 R5 N
6 v* {. p# m, _( q q7 u( e1 @6 }5 l/ G# C% f) f3 y4 \9 S
|
|