|
|
浏览器访问:
1 n J7 D' n0 W* _! qhttp://127.0.0.1:5000/hello/render/str/8 W0 i5 |9 k2 P1 y
结果:0 Z' S) I/ p/ K/ u# x6 d8 l& a5 A
TemplateDoesNotExist at /hello/render/str/
* ]. { M0 Y: lindex.html$ _2 p1 A- b' t' Q
Request Method: GET
5 g8 N N1 p# e! m9 fRequest URL: http://127.0.0.1:5000/hello/render/str// Q" Q9 f5 |6 P& m
Django Version: 3.0.56 q2 j O. K9 X& x! @* ^
Exception Type: TemplateDoesNotExist
4 e: ~4 w/ _) K3 f# cException Value:
2 M4 X6 A8 `9 a4 X hindex.html
8 r' X# ?: q+ U# B6 jException Location: C:\Users\long\AppData\Local\Programs\Python\Python38\lib\site-packages\django\template\loader.py in get_template, line 19
% B, x7 m+ M& UPython Executable: C:\Users\long\AppData\Local\Programs\Python\Python38\python.exe
. i( V* \' ` J J% v% wPython Version: 3.8.8% B7 x( |0 C" e: g& F0 _) O1 K
Python Path:
. ]4 [/ {3 f! X1 }+ E" y7 W['C:\\Users\\long\\Desktop\\my_project',
# m8 u/ s% N9 }: T E/ u 'C:\\Users\\long\\Desktop\\my_project',2 h+ e" m3 T, L y
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',3 ]5 X: }9 Y7 A- b/ H6 |
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',. n3 A: F; c/ u$ d) U
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib',
+ b5 Z0 L' ?8 x- l 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38',
% o' _: F4 L9 o2 p8 \ 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']' _# @2 U" Z) i7 R
Server time: Sun, 6 Apr 2025 11:17:52 +0000( ^/ R0 ^. v9 Z7 R% J% Z; l# ~
f% L' W/ ^6 T" d0 n提示来看,是因为找不到templates的文件。6 j k( r* \9 S& T [% _
5 P/ d0 c9 t( Z; q找了很久,也没有找到好的文档进行说明。最后抱着试试的态度,修改下项目目录下的setting.py中:7 c% h7 w; L9 }; Z9 M' G
TEMPLATES = [. h+ J8 i8 k+ b& p3 [1 |6 D
{6 P8 \% `1 n' w
'BACKEND': 'django.template.backends.django.DjangoTemplates',5 F/ l* v O$ }4 X9 S
'DIRS': ['templates'],
' s% F2 I4 e' }5 l# L 'APP_DIRS': True,
1 ~4 Z$ E& m% w" \* P7 t 'OPTIONS': {
) _0 N4 x# ^ ?$ L0 M" f 'context_processors': [
3 g6 }* A3 }& s6 W 'django.template.context_processors.debug',! ~) ?5 C! x6 Q& W+ x
'django.template.context_processors.request',& f0 Z/ U, q. h( m
'django.contrib.auth.context_processors.auth',
5 X; r: x5 E3 O# F, ]" A 'django.contrib.messages.context_processors.messages',' H' a' T4 H: r$ _
],
3 O2 u) k8 y, U( x& \% R },
6 w/ t( i. q! M# c },
. @. t/ G3 j; ]' a" G! x) }0 B]7 m ?$ V' l: u/ g; c
改成我们项目的一些配置:如下修改:
! y% ^" U$ i1 A% L q9 ]% F" nTEMPLATES = [: C8 b" q' m4 q7 q
{$ J" }3 S# X0 u
'BACKEND': 'django.template.backends.django.DjangoTemplates',
$ u% f: E$ n2 M( I- E/ _ |% A4 K% u5 t 'DIRS': ['my_project/templates'],
1 u" y6 L \, g( u9 C 'APP_DIRS': True,
; B: r3 U. V& K7 f7 ~' \# } 'OPTIONS': {3 r- j+ A# g4 V6 m: w3 ]
'context_processors': [6 h% b7 O9 `2 M% R. L% `
'django.template.context_processors.debug',1 u3 l0 F% F2 R* T8 ^
'django.template.context_processors.request',
3 N H9 ~. T* s" f; J 'django.contrib.auth.context_processors.auth',+ V/ H/ X: Y. |5 ^3 w, u2 s
'django.contrib.messages.context_processors.messages',
: r+ S+ w v' I1 v' I ], H; m0 e$ V5 f- r, Z, U0 n
},
0 `" m& D+ @9 l* t },
: ~9 y7 \; d: N8 I]7 [" y1 ^& ?$ p M( l8 [, z
再重启manage.py 服务: python manage.py runserver 0.0.0.0:50001 M) i4 ^0 A; l, @& `! g/ g2 L1 b
再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签# T5 }: ^& b: f2 s
A1 i6 P: @& `! X8 b" V问题解决。
8 Q3 F4 G* @+ Q$ `# v% N. ?2 N" }) F# g; w' H9 a+ @8 ~# D
; h& L, T# k$ N% [+ U
|
|