易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 166|回复: 4
收起左侧

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

[复制链接]
发表于 2025-4-6 19:33:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x
浏览器访问:
, T& Z8 Z+ D5 }! n- nhttp://127.0.0.1:5000/hello/render/str/- T# G4 u/ l3 b' |# D' [- \/ x
结果:" x- Q9 X- H4 [) m
TemplateDoesNotExist at /hello/render/str/4 C* s# j" P1 d: z5 f
index.html
1 q! G; i" \4 yRequest Method:        GET) {8 w. s6 \6 C( K# ~6 R
Request URL:        http://127.0.0.1:5000/hello/render/str/
1 r) G! u( O+ A# JDjango Version:        3.0.5+ q# m- y6 |7 d" U  Z0 F
Exception Type:        TemplateDoesNotExist3 d( |/ f5 Y8 r% ]# S% ]
Exception Value:        * {6 p5 f4 @$ M
index.html
3 M4 g9 H$ q5 W0 m3 h1 S  GException Location:        C:\Users\long\AppData\Local\Programs\Python\Python38\lib\site-packages\django\template\loader.py in get_template, line 19
6 F/ h4 {* V/ j3 d: sPython Executable:        C:\Users\long\AppData\Local\Programs\Python\Python38\python.exe) m2 X' R# W% |1 r  C
Python Version:        3.8.81 @$ O8 n( A" }, `2 ?! k: }
Python Path:       
$ y& W/ E4 q8 I# g['C:\\Users\\long\\Desktop\\my_project',3 s( ?% C( T% o. P4 y
'C:\\Users\\long\\Desktop\\my_project',
* V8 o2 A) K5 q6 M2 t 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',
* l$ o7 }- w# {4 D) K 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',
) s, r  G8 I0 b" P2 Y; f2 W 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib',
/ H) n4 p1 N( K- z 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38',; L8 o, D9 E" N* }; Q7 q) v4 n- [* m
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']
) b# T8 p8 u. [2 v( a' L( _8 o: gServer time:        Sun, 6 Apr 2025 11:17:52 +0000
& p% V& u3 n, N
4 X7 C0 ^3 R7 c' [提示来看,是因为找不到templates的文件。5 G3 m& x# e; `( k/ z. q
. y2 b! b2 ~' q
找了很久,也没有找到好的文档进行说明。最后抱着试试的态度,修改下项目目录下的setting.py中:5 E( \- o* e5 ]3 l5 k
TEMPLATES = [; v6 m( r8 M1 W4 {
    {
9 S4 Z# E# c# M, N1 t' b        'BACKEND': 'django.template.backends.django.DjangoTemplates',1 u7 |' W. w# o4 Z
        'DIRS': ['templates'],
% G1 M  L- c" f6 I, x) \0 @, g# K) k        'APP_DIRS': True,4 o, `3 t) B. x9 d8 E. m
        'OPTIONS': {
: L) Q4 i7 J7 h  |0 N0 \2 y3 g            'context_processors': [
6 B" q& v# [; F% o                'django.template.context_processors.debug',8 h6 n. T6 Y5 x5 m- h# [% i: u: K8 M
                'django.template.context_processors.request',. h; @. a( n% }6 W0 j1 q
                'django.contrib.auth.context_processors.auth',$ F9 z* o$ g: l+ K; I3 |6 T- T
                'django.contrib.messages.context_processors.messages',( e) e2 f0 A( t; F9 _2 ~" |
            ],
" d1 h8 C0 r+ ]1 B; x+ r) y        },8 @% ]+ F3 C1 Y% S
    },( ]% _/ |, G' A3 A( D
]
+ W% g+ B* B5 O6 D8 M2 h/ ~改成我们项目的一些配置:如下修改:
" |+ h- P7 w- J8 L1 Q4 G% KTEMPLATES = [
0 ~1 q' H* t2 W3 W' X* j; T    {# F1 _' G8 W: F! s' _! R2 ?
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
/ k( q% ~  [# M" t/ Y* J/ z        'DIRS': ['my_project/templates'],
  h* f( C- |7 T" ^% u4 [        'APP_DIRS': True,) m, ~! C" T' k& n" q
        'OPTIONS': {
' x0 k) e6 [* j2 Z            'context_processors': [
7 M  S& Z" Q. n* X  Y4 q: \                'django.template.context_processors.debug',
7 l$ ^0 J7 i8 q  l7 a  O                'django.template.context_processors.request',
9 c. n4 S$ B# V. f9 ~( X                'django.contrib.auth.context_processors.auth',
+ [' M# N! d7 w                'django.contrib.messages.context_processors.messages',. E; ~3 O5 V. M: x" y+ x4 t" W* A
            ],7 ~  @0 Q# F6 V; d+ Q8 t
        },. u4 M6 T  f  T* |
    },, r6 T) U* Y0 s# D) K- j/ ]( f
]/ t. ~, t2 b5 E
再重启manage.py 服务: python manage.py runserver 0.0.0.0:50001 M$ {  Z$ S) K" P9 {
再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签
/ ~$ e- Z! Z; h% z
% j: \$ ^! ]; A2 |2 d3 b问题解决。1 p% M. N: v, C5 N5 v

5 ?, b) I! H. m! z+ J( Z: ~8 \9 |7 {0 }  ^+ e6 x* X
 楼主| 发表于 2025-4-6 19:36:32 | 显示全部楼层
有地方介绍可以使用这样的方式解决:+ Y$ i2 O6 ?8 r0 p, c$ E
修改setting.py的配置:3 T& }* G# v2 w3 n1 q7 H5 F

3 ]" u! p4 I0 N+ oTEMPLATES = [) f4 O8 D1 M- ?8 T( G! X2 F0 `
    {
) K* e! q2 I; J/ M  p6 v3 Z% `        'BACKEND': 'django.template.backends.django.DjangoTemplates',
, j/ |1 w# D0 S4 ~( v, ~        'DIRS': [os.path.join(BASE_DIR, 'templates')],
4 j9 Z6 D# {6 E6 b        'APP_DIRS': True,% N" X' g- r! _2 V% |  J# |8 V
        'OPTIONS': {& G6 D* r' ~+ f' ]  l& h
            # 其他选项...
1 k2 S- o4 S  |# A# a" |8 X        },5 z7 V$ V# L$ ~) s: i
    },# [2 B  o0 v9 ?+ [
]: ]/ ?# f* G. b. ~+ D! h5 u
7 C- k$ B) C9 O- u
使用参数去获取目录:; G, |- }, M1 L7 Z) n
os.path.join(BASE_DIR, 'templates')( o( d! u9 E- z" l" \5 g5 p6 i$ B
( Z, g5 l4 V3 B/ Q5 T1 D. K& e) O$ s
测试结果依然不能解决问题。# @, s. Y2 p. _) w5 l3 B
还是楼上的问题。 ! N" k( t- Y" {! d8 P1 o# |. K
7 b( Y% U, Z3 k. S
最后抱着试试的态度,修改下项目目录下的setting.py中:& A1 b& v" u+ i0 L# A, k
TEMPLATES = [/ B$ D# l5 h3 r0 @7 G' o4 g/ M
    {
5 i6 J8 @& g8 ]& @        'BACKEND': 'django.template.backends.django.DjangoTemplates',6 b) z+ p$ W0 l* f6 m! S% a" p
        'DIRS': ['templates'],6 i6 I3 Y+ @, c; E. O
        'APP_DIRS': True,
/ S2 I  N; J) |        'OPTIONS': {
/ z8 d1 G" }8 A: Q            'context_processors': [
) v. a. G! [2 Y  i+ E+ Y                'django.template.context_processors.debug',7 ?' w5 c/ v5 |6 I  }+ Z4 P+ D
                'django.template.context_processors.request',; ~  L7 u5 Y$ M( \+ N) ~) q1 Y
                'django.contrib.auth.context_processors.auth',, D+ U0 k3 N+ s4 o7 G
                'django.contrib.messages.context_processors.messages',/ N# `4 @) u" ^. e$ {0 `
            ],
) Y4 p" x$ u, |2 B" |        },
. M8 A/ V3 `0 k" y' u% L% T: `, Y5 I    },
  n8 b0 I6 e, D2 s9 L% C]
+ h2 u- q( B2 u# ~/ E- U. P2 i1 R改成我们项目的一些配置:如下修改:
" R, f2 V5 |; a7 q% lTEMPLATES = [
6 j) ?+ y9 y/ e4 W6 r- ?; K    {  z/ L" r* ~, j5 M* w
        'BACKEND': 'django.template.backends.django.DjangoTemplates',) f- C  E- M" k& d! ^
        'DIRS': ['my_project/templates'],; m, Z9 \& n4 B+ B  o! \
        'APP_DIRS': True,9 b2 J! g8 ~# S
        'OPTIONS': {( r' D+ v; P6 y' o; L
            'context_processors': [) L( T0 G$ N- j. i) ?; V# _
                'django.template.context_processors.debug',
8 L& f6 L8 K/ `3 s                'django.template.context_processors.request',& r1 o3 B5 I$ }! Y% B. N" ]. |
                'django.contrib.auth.context_processors.auth',
0 f1 J0 ]9 w. C% B                'django.contrib.messages.context_processors.messages',6 t  A' Q' _& F/ `9 k
            ],- M9 ~7 K2 [2 n; z
        },
' k" G2 u$ c3 O$ `    },
/ B5 H  j% @6 P/ p8 s]6 P/ K1 p4 Y' B# s
再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000
& d) m/ a" N' ]8 L! s  R再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签
3 h' y5 _2 X# e' ]+ B! Z0 ]1 }6 N$ g- O! T  J  e
问题解决。
* j9 B6 Y; }# b5 H! D) T+ v, h5 q0 N: ^* U6 d: M0 H$ K. A
; Y& r4 A3 G+ M0 @& f1 }
 楼主| 发表于 2025-4-6 19:37:19 | 显示全部楼层
使用绝对路径4 q, O* T# `  F' j9 }0 ]
在模板标签中,尝试使用绝对路径来加载模板,例如:  U1 H1 I$ G* I8 s# \7 Y- c) b# h
   
3 H6 ~' P7 Y. a; @# B{% extends 'app_name/template_name.html' %}
. x+ P5 I2 \, c5 j确保替换app_name和template_name.html为正确的值。
 楼主| 发表于 2025-4-6 20:55:49 | 显示全部楼层
后来测试,还发现一个问题,这个地方配置其实是没有问题的,
- C0 Y! @. G5 t" i: [2 M6 K# a9 wTEMPLATES = [. \3 y4 ^; v2 c2 t: u% ~- o
    {5 v6 s. |' Y" H7 o
        'BACKEND': 'django.template.backends.django.DjangoTemplates',0 s  M+ ~4 S) Y5 H" W6 e% U
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
+ u. F' m+ o8 h  S/ V. x9 \7 V/ D        'APP_DIRS': True,+ `7 n; D& M% s+ s  T0 h
        'OPTIONS': {, e# O: L* L6 p$ _7 S+ U
            # 其他选项...# l7 X3 o  _0 s& D, [( [# P' q
        },
( ]6 g; n5 W: P    },
) \- R- n& \4 t9 s1 z4 s]5 T; x: J- w0 w6 G) J6 d) P

1 h3 F2 U' I8 T2 w/ w+ M1 E而是忽略了一个问题,需要把templates这个文件夹放在大根目录下。
* ]7 S* @' S3 @( Q' `# V' s* g
 楼主| 发表于 2025-4-6 20:57:36 | 显示全部楼层
这样写:1 u6 L2 e1 \! A6 h$ ]
'DIRS': [os.path.join(BASE_DIR, 'templates')],
5 Y9 L& ]4 V3 }: X+ N" M7 Q  o9 I, u+ ^或者这样写都可以0 _! e4 O- q- R
  'DIRS': ['templates'],3 C: q- O$ ~$ B

* S& A6 E5 }3 z/ x; }& W6 r$ `6 P会报错是因为没有注意到是否在根目录下。
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )点击这里给我发消息

GMT+8, 2026-4-8 21:34 , Processed in 0.058159 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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