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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

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

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 19:36:32 | 显示全部楼层
有地方介绍可以使用这样的方式解决:& o' @# t4 s. y5 o# Z7 S
修改setting.py的配置:7 m' f6 l# B1 f2 V
. p  P/ k6 H+ L7 P
TEMPLATES = [
! [% n1 Q# F% H5 l+ x5 t    {
/ F" g% U% @. t3 v. H4 X        'BACKEND': 'django.template.backends.django.DjangoTemplates',/ U* W* H3 o- a, P
        'DIRS': [os.path.join(BASE_DIR, 'templates')],4 Y- I" R9 ^9 @2 t" q4 _
        'APP_DIRS': True,
- Z$ s. p3 |" P2 N: `6 S        'OPTIONS': {) Y8 ~: }5 r! o2 i1 Y
            # 其他选项...
$ b# L8 a6 G) J% g( D& X( \+ |        },3 U) L/ f- E$ D2 y& \: W) j& N" g
    },
% O9 t) V: j$ ]) ]]( i: t. ^0 c0 J, h5 F4 G

9 [( A( n$ C. k8 @( T使用参数去获取目录:
: b) q$ w5 V, @os.path.join(BASE_DIR, 'templates'), p$ {+ J1 s, X
! \9 i: L# Z, Z4 B0 y% v+ e
测试结果依然不能解决问题。
0 s# ^* r0 a1 C5 y' a" B$ D还是楼上的问题。 / e! T; |6 d/ O, a

) w: \  i5 Z- |% Z5 Q最后抱着试试的态度,修改下项目目录下的setting.py中:
' C0 X4 U8 e& B- ^TEMPLATES = [* B# T8 D  p, \7 J
    {
' w1 t& C# d& G        'BACKEND': 'django.template.backends.django.DjangoTemplates',
7 m5 w3 H# _0 B1 b% @% \        'DIRS': ['templates'],; e* Q) w$ d5 T6 T2 r& N
        'APP_DIRS': True,
% d: T7 }1 q/ g! Z! u: u        'OPTIONS': {) k+ C: @3 i5 x$ @0 F' n
            'context_processors': [$ l% V! X7 \+ q, I& c) |
                'django.template.context_processors.debug',
# j' Q9 r  f# C0 U) L% k                'django.template.context_processors.request',8 D( r7 q! _7 m8 E2 y8 q0 b
                'django.contrib.auth.context_processors.auth',
/ W0 {5 H# u7 l. D4 }                'django.contrib.messages.context_processors.messages',8 H3 @$ R7 Z9 o# ]7 H
            ],
; }; n- X5 l" |7 g        },( G  _7 @+ H. |! z% q
    },0 f8 x9 I* z8 S# g6 ~# ~3 f
]
1 p5 f( o: ?. @. n& w" i5 p! W改成我们项目的一些配置:如下修改:1 \+ y- k; `& D6 ?' ^
TEMPLATES = [
4 K: t& g4 F1 {% I" M    {* L/ {& j' S; R  ^6 k* S: ~
        'BACKEND': 'django.template.backends.django.DjangoTemplates',' s7 F+ ]5 e3 d
        'DIRS': ['my_project/templates'],
* z6 n( n+ ?% G7 b% w3 G        'APP_DIRS': True,
0 z/ S" M  o  M( k/ m( G9 c5 n3 Z        'OPTIONS': {+ d1 k! I- d1 X- V8 J5 F# A
            'context_processors': [' j5 Y  F* S0 u# @; h
                'django.template.context_processors.debug',
$ N' e+ W7 F8 t                'django.template.context_processors.request',
% t8 K) s- W& A/ N8 l; L" R                'django.contrib.auth.context_processors.auth',
: _4 _1 e1 B6 R                'django.contrib.messages.context_processors.messages',6 r* m7 w$ g7 V( G; I/ z; B, ^: l% r# ^
            ],  a! y/ f. n: d7 B9 x* E
        },
  \0 K- k2 x* T7 p' S    },
4 Y8 d7 [0 W: x, |. R' p. h9 S]- H' m) }& V9 I. \* v0 S6 N4 o
再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000
4 e3 {1 k- T) R* X/ m, L再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签
7 T( g: f. a2 [7 @! O: I
) p$ G% O8 H  [& a" I6 G6 {+ v2 L问题解决。8 o( r9 d6 F6 d5 c: j# T

0 N/ O# }' W% q: t7 }( @
+ Y0 C2 }0 G8 C9 Y* y. M4 g

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 19:37:19 | 显示全部楼层
使用绝对路径
1 P: e2 q& ]" v2 o; \: i在模板标签中,尝试使用绝对路径来加载模板,例如:9 C. [8 K( L! e2 G3 U
   
  Y- x, T1 k1 w' @4 W" L# X{% extends 'app_name/template_name.html' %}
  |1 h2 M4 n9 h确保替换app_name和template_name.html为正确的值。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 20:55:49 | 显示全部楼层
后来测试,还发现一个问题,这个地方配置其实是没有问题的,/ S& \" P" F1 }: i
TEMPLATES = [
2 [, ?# i" H3 }$ q, y2 `; S    {
& \) M) U" T# j8 b. X# L1 s        'BACKEND': 'django.template.backends.django.DjangoTemplates',
( ]0 Y4 \" B' C- H# w( O* O        'DIRS': [os.path.join(BASE_DIR, 'templates')],
& L. l3 `' j- a* e        'APP_DIRS': True,
' j% a  C  i! C- C, o; K        'OPTIONS': {
$ _" v. V( @# n8 a- v1 ?            # 其他选项...
$ b1 \2 o' L8 q$ R' Z  m        },9 T3 t) W+ l9 v2 i8 z* A
    },6 l: \9 |. k  Y! _- N5 Z  W
]
# A7 p; o7 ]$ z0 h8 B) G
0 s/ y8 e5 t. F) G! z* F而是忽略了一个问题,需要把templates这个文件夹放在大根目录下。
' Z9 l- Y5 k5 U1 E1 ]& \& x

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 20:57:36 | 显示全部楼层
这样写:
2 E$ ^2 I/ }1 N; P'DIRS': [os.path.join(BASE_DIR, 'templates')],8 Y3 N- }8 X3 Q7 a7 g
或者这样写都可以
# w8 L; C! V& b( `  'DIRS': ['templates'],: s5 y% q; |; |# X& B% o1 K5 w  Q

' O/ V6 L/ i9 Y- H+ j会报错是因为没有注意到是否在根目录下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-11 23:15 , Processed in 0.025588 second(s), 21 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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