- 积分
- 16843
在线时间 小时
最后登录1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?开始注册
x
浏览器访问:
i( S& W3 U6 H' t: p9 q g4 Nhttp://127.0.0.1:5000/hello/render/str/+ {: x/ s( ], H7 p8 p1 W; l
结果:7 A" {8 G3 o* w* h
TemplateDoesNotExist at /hello/render/str/
+ ^+ N" ~: P! X9 Z: Aindex.html7 z6 [) j# v% z/ _1 \
Request Method: GET
3 r2 E2 T0 Z$ e$ B. e" t" \3 e$ iRequest URL: http://127.0.0.1:5000/hello/render/str/
9 F5 _, x1 o' `( U, V- vDjango Version: 3.0.5- Z: E7 }! l: J8 }; o
Exception Type: TemplateDoesNotExist
5 l2 J, S- \' A0 u1 @Exception Value: [# ^4 J. d& Q2 \# b* M
index.html+ l1 |, I" H1 ]. f
Exception Location: C:\Users\long\AppData\Local\Programs\Python\Python38\lib\site-packages\django\template\loader.py in get_template, line 19
( g0 l( p- ^! c2 _$ _, RPython Executable: C:\Users\long\AppData\Local\Programs\Python\Python38\python.exe
" j# [/ B( h5 t7 h# v: dPython Version: 3.8.8
, t' j7 H" \$ t) @- h7 t/ fPython Path: . a( S& _5 G5 X! I! c3 J
['C:\\Users\\long\\Desktop\\my_project',- ^2 T$ \# V! L/ y: U
'C:\\Users\\long\\Desktop\\my_project',
' }; i8 {5 o |2 x( n' L, _ 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',, a* g9 h$ o# c+ ~! c, s& v
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',0 }* |: Z8 ?6 f! y, }" Z
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib',
0 A* j, R8 F: v; J7 c 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38',/ q% p+ I E8 A7 P* A2 L
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']" ~" L% m5 q* Y: b* \
Server time: Sun, 6 Apr 2025 11:17:52 +0000
7 ^5 S' R9 c/ R# }1 ]! H
' z, N, _2 U7 j3 t8 u' b& d$ F+ O1 ~提示来看,是因为找不到templates的文件。9 ?, U* T W) N+ Q7 u3 B3 l
4 m* l: f: T! C& R c找了很久,也没有找到好的文档进行说明。最后抱着试试的态度,修改下项目目录下的setting.py中:8 c& d2 i! Q' G* r. B" X
TEMPLATES = [% L/ ^ D9 g J) u1 G8 c% e
{" y0 x% r1 G5 m
'BACKEND': 'django.template.backends.django.DjangoTemplates',
$ N9 U0 Y5 M3 Q9 E' G 'DIRS': ['templates'],
" [6 j ]2 h+ q* z9 }! Y& c 'APP_DIRS': True,3 b( I4 J+ n( O- ?5 x3 \: O6 Q" _) b
'OPTIONS': {
# P$ a" b0 z- D- V$ \0 g 'context_processors': [5 n4 S( O; O% `; d9 s! Z3 c) L
'django.template.context_processors.debug',4 A- ^6 B8 i; s% s
'django.template.context_processors.request',; n; ~- z" O2 p. }
'django.contrib.auth.context_processors.auth',; T# @9 i$ h- P7 a: ~* ^. ?+ q& j
'django.contrib.messages.context_processors.messages',
0 q/ @* W2 t8 Z \ ],
% c3 w) Q- W$ i+ p4 P },
9 t3 o$ B; o$ ?+ h7 [ },
" I1 y* t* Z9 `6 z, y6 ~6 V]+ s0 G" R7 \ _7 p! ~6 P1 I, }
改成我们项目的一些配置:如下修改:
2 R* ^# l9 {4 ^6 uTEMPLATES = [ z+ O1 M6 Q6 `* X) x
{; a* a$ f. z) o3 _% c
'BACKEND': 'django.template.backends.django.DjangoTemplates',* g; D2 _9 ^0 z9 W/ q
'DIRS': ['my_project/templates'],
2 o Q9 F) V1 u 'APP_DIRS': True,
0 Y, ]- ^4 B- \: ]5 m* o4 V& g 'OPTIONS': { S4 `" O7 |% a8 p% R
'context_processors': [
& M) ]! k* ?% j" X* {( ^2 Z$ K 'django.template.context_processors.debug',: Q# b r5 p% V- F1 K0 H, b
'django.template.context_processors.request',
" X2 A' f2 x# s* X) ^' x" [2 w' K! O 'django.contrib.auth.context_processors.auth',
/ b" `; n: k# V9 g+ X6 h* B2 w 'django.contrib.messages.context_processors.messages',: E3 M9 g6 ]# l8 b% q0 v
],& |# P1 T1 R5 I7 Y+ @4 I
},1 \+ E% [& o' F
},% a3 n# ? b! w1 j- L7 i
]
& c9 v/ v5 B3 G+ `; S再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000- @1 C- J* C: i6 j, }
再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签
" [: l" I9 h( ?6 c7 a2 B1 E0 }% a c) Y5 C. R
问题解决。; ?, L0 c- ?: |$ b* v; Z
) B/ e& V& e) E, Q8 w5 b; n1 a% x% {
|
|