|
|
浏览器访问:
3 o, i4 P! G0 U: e! E( K3 ihttp://127.0.0.1:5000/hello/render/str/ O, Q( @/ ?9 K* h4 Z N) K
结果:
- @- D3 R: x! L0 ]3 j8 `TemplateDoesNotExist at /hello/render/str/
7 w# w: c2 Q/ [. Lindex.html. _, i" F0 J9 }) Z9 g
Request Method: GET/ L: N3 a# w0 }
Request URL: http://127.0.0.1:5000/hello/render/str/
( E* o, N. @6 Q" ?Django Version: 3.0.5- r+ u! t% M. N. \1 g
Exception Type: TemplateDoesNotExist0 j+ x: U* j( |7 m# T2 k# [
Exception Value: * q9 A. n+ t1 M# H/ Y- l! \
index.html3 @- a/ c* D# D A' u
Exception Location: C:\Users\long\AppData\Local\Programs\Python\Python38\lib\site-packages\django\template\loader.py in get_template, line 19+ j: j! m: m; `( V, |
Python Executable: C:\Users\long\AppData\Local\Programs\Python\Python38\python.exe
0 H5 Y, b# M; wPython Version: 3.8.8
^/ S. A" c1 a# [Python Path: ) U& {& R; @4 J% Y5 Y5 q
['C:\\Users\\long\\Desktop\\my_project',: F# i& p! C$ a# u& y F
'C:\\Users\\long\\Desktop\\my_project',
Y7 F0 Y( Q9 z u+ x Q }' p 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',( _, J0 W* L* U1 `. L0 e Y
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',
: |- o' q6 u$ P1 }0 Z8 T& c/ ]! E 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib',' ?! o! I3 t# p4 n- D
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38',, F* b/ r# R0 |# h; o$ [
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']! m" Q, }/ p8 L, `
Server time: Sun, 6 Apr 2025 11:17:52 +0000) ]0 L! H- |; B+ F3 r
+ J( i$ ~2 \* O/ m7 o. {
提示来看,是因为找不到templates的文件。
& U2 B# S) |8 j$ W0 A6 R/ C5 Q2 ?/ z8 t
找了很久,也没有找到好的文档进行说明。最后抱着试试的态度,修改下项目目录下的setting.py中:
o7 ]/ v7 b+ ~- R! N' Z3 D5 a% yTEMPLATES = [
$ }. a& _+ A# j# @- L4 Z {! q' K7 S9 ?' h
'BACKEND': 'django.template.backends.django.DjangoTemplates',
, h4 j' G& E0 U 'DIRS': ['templates'],: L. j0 M* |; }0 X5 k3 h$ f
'APP_DIRS': True,
3 K+ i: y- B- }. [) o 'OPTIONS': {+ r9 C+ Z9 e% l. k
'context_processors': [0 d2 L' X# @% `3 ], f) w' |% d R
'django.template.context_processors.debug',
* @8 j$ A# U- v0 o* s 'django.template.context_processors.request',! D1 [3 j5 C$ F/ y9 D
'django.contrib.auth.context_processors.auth'," \* O# u1 L: p* v
'django.contrib.messages.context_processors.messages',$ }* ^, d0 c0 o# y
],
. r! n N* K) _' F7 c( E },
& g$ \" J9 @+ e% ~ },5 ]5 W5 |/ }' L1 ?2 C7 G6 ^
]
! ?7 T7 W. q/ j( l% E( U改成我们项目的一些配置:如下修改:
5 A7 E& r2 L1 U2 Y( e+ \7 q RTEMPLATES = [2 v5 r( Q }: f" W6 _/ q
{
6 n5 t% [: A3 u! H- Y1 c 'BACKEND': 'django.template.backends.django.DjangoTemplates',- |5 t" i4 n' K2 u" v5 l+ a7 C
'DIRS': ['my_project/templates'],
% e4 M* L* K/ }; k. n 'APP_DIRS': True,0 ^# d: P' T. y" E c+ A, R
'OPTIONS': {- z) r& y# r: E
'context_processors': [) @6 Z% m9 c# z5 E" S) w
'django.template.context_processors.debug',& B1 d( w" w$ |1 r
'django.template.context_processors.request',' e0 {# n5 |5 |8 k- h
'django.contrib.auth.context_processors.auth',
- R0 A. z8 k$ v$ { 'django.contrib.messages.context_processors.messages',
) s0 W* E" y8 ~0 G ],
- ^/ s2 {' ]6 p% B: ^/ ^ },/ I' k5 a6 ~; b @% a6 o( ^, c7 b
},) W( Z% X" _/ s8 k
]
) W9 G3 v$ _( }2 R/ l再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000 ]5 t! t- s0 Q7 p8 u
再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签
: F6 v/ P& e. o5 T `' P! u
; b. [( `6 R, G$ ^问题解决。- a# H8 B% ?: }' R: l
- C. X! Z1 v* [, \4 ^: o: I. q; N/ D
; g3 V0 i( L8 k M |
|