找回密码
 注册
查看: 172|回复: 4

TemplateDoesNotExist at /hello/render/str/ 解决办法

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2025-4-6 19:33:18 | 显示全部楼层 |阅读模式
浏览器访问: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

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 19:36:32 | 显示全部楼层
有地方介绍可以使用这样的方式解决:) K  J: j# y' h. ?6 A6 g
修改setting.py的配置:
0 Z' W2 w8 c2 w% k8 J+ q5 l9 p9 L6 q$ _( w3 R
TEMPLATES = [
' W2 K$ E; Z; @( L" T% \    {
# S  r2 m% Q0 B! _+ R  W( @  I        'BACKEND': 'django.template.backends.django.DjangoTemplates',
9 G% E' M0 k0 V& f  v& T& w        'DIRS': [os.path.join(BASE_DIR, 'templates')],0 ^  O7 W: u/ m, i) D- |2 x+ f
        'APP_DIRS': True,2 H- z, f; e- L( ^0 T+ E
        'OPTIONS': {+ L2 M  ]" V; {) _! C
            # 其他选项...7 ~5 g: U  W* i$ Z. i+ R' {$ M
        },/ v+ T; S6 L! s' ~8 l/ k2 J! m
    },
$ z5 V7 y# @! a]
. ]5 ]* R- \7 T/ ~6 z, B7 j; y: ?: r5 B1 j
使用参数去获取目录:
4 V, z. s' f  F0 \3 E2 Yos.path.join(BASE_DIR, 'templates')
" Q8 ?% u. N* l  _! O
8 c8 m( ^8 q/ I6 X" {2 S/ n测试结果依然不能解决问题。
8 p4 s2 \) |8 o) T7 L7 ?还是楼上的问题。 ' j6 R& K# y* T- y4 j
9 o) c7 v. k, O! |) M
最后抱着试试的态度,修改下项目目录下的setting.py中:
$ O+ b$ ]" S  QTEMPLATES = [
! y! S' w! |( u9 c) z    {' F- X7 s' X; v) |; B
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
+ i4 ~) ?$ \1 t4 B        'DIRS': ['templates'],
; C0 k9 w6 h! b8 u) [$ P        'APP_DIRS': True,/ e! Q$ p0 W* F/ b
        'OPTIONS': {2 [; u' R/ g5 E) t: v2 S: b& @
            'context_processors': [
: e! F1 G  J# t# M+ t                'django.template.context_processors.debug',$ e; q" t  e0 z' {# k% S. B
                'django.template.context_processors.request',
- J; o, ^; ~) q0 `+ U. Z                'django.contrib.auth.context_processors.auth',+ i3 M" a% A3 ~3 T$ w) K/ h
                'django.contrib.messages.context_processors.messages',0 a; |, H. I' ?/ `: G
            ],
4 ^1 T4 _1 q' N        },
6 V, F4 G- p( }) D    },
0 S' L' G; m9 I  ^0 f5 M]$ b: q: `% i; L( `3 r
改成我们项目的一些配置:如下修改:% O$ z- {$ D# ]1 u8 r/ H1 d) t" T
TEMPLATES = [. a3 a' q1 t. `. }+ y. P% ?
    {- z' N4 N7 z# g& ?; G8 H( N1 G. Q
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
7 O) N! x6 x! {: n7 T7 m        'DIRS': ['my_project/templates'],. r$ ?+ {3 [4 w5 }9 Y% b( b
        'APP_DIRS': True,
( v8 l6 k: s3 d        'OPTIONS': {
" m4 p" F' y8 [" `; u) I$ ?* s$ _            'context_processors': [
. @7 w' t6 O, c                'django.template.context_processors.debug',; d7 \3 J) ]4 L' N$ M* ^7 o/ d
                'django.template.context_processors.request',
+ @$ h" k2 g& F1 U0 N$ A" L" Z7 O                'django.contrib.auth.context_processors.auth',% i" l2 L- r7 Z5 o
                'django.contrib.messages.context_processors.messages',7 ~# e, ~0 C" e; E, @" h
            ],
- `/ i' Z& w$ B5 m3 i, G2 @8 d        },
) p3 q" i  w8 `( t  P    },9 Q- k6 b! ~9 r
]
% a/ B4 F/ E: k3 z再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000' \" y( h9 X6 q5 y! g$ }6 Q9 L
再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签
, x/ y1 x( i( p4 ^; n: {( K. u5 L4 l5 q/ H
问题解决。
2 Z. L6 s* m$ p5 Y6 e& K
1 ?" R7 ~$ f+ n- c+ m8 Y" z7 L& p, C7 V: x& K: a

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 19:37:19 | 显示全部楼层
使用绝对路径
4 O9 l& @6 h/ w! @4 X" H. p在模板标签中,尝试使用绝对路径来加载模板,例如:
+ a! l6 [* r& U5 s   ( X% B  h: _+ C  W8 M
{% extends 'app_name/template_name.html' %}$ n: W/ w$ t( J
确保替换app_name和template_name.html为正确的值。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 20:55:49 | 显示全部楼层
后来测试,还发现一个问题,这个地方配置其实是没有问题的,$ X" ~( V0 {9 x" z
TEMPLATES = [
; D& N3 H, e# T, S0 Z! y    {
$ b" \/ \- \- ]! X& D1 v' W        'BACKEND': 'django.template.backends.django.DjangoTemplates',' }3 a5 h: {4 ]7 U1 |5 _- }
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
2 Z4 |1 L! @2 i2 g% a6 q- S# k( f        'APP_DIRS': True,* Q) t8 t5 F- F5 |- p' p
        'OPTIONS': {
  K4 m( G" n7 x/ G' }9 G            # 其他选项...# H1 B5 R/ ]/ k, N( ^+ M& P
        },
& B2 h2 c7 f) j* S. S/ g; L    },* c" z# ?/ z4 X, C) C) N! @
]
$ U( m% H5 L9 h+ O6 P6 g' L1 i' T% Y
而是忽略了一个问题,需要把templates这个文件夹放在大根目录下。% d) a2 H0 T0 Q7 c0 z

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 20:57:36 | 显示全部楼层
这样写:
+ [) z+ \3 L+ R7 E( y'DIRS': [os.path.join(BASE_DIR, 'templates')],
' V$ b" c" W( N4 E% f或者这样写都可以3 a6 h2 |5 c. v3 c6 e* v* v; f" y2 I
  'DIRS': ['templates'],2 Y7 G# y5 A% e+ C: J/ L: r
# w+ ~$ M% Z4 x2 n4 `' K/ R+ {* L# r
会报错是因为没有注意到是否在根目录下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-11 23:20 , Processed in 0.019656 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表