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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2025-4-6 19:33:18 | 显示全部楼层 |阅读模式
浏览器访问:9 j4 F9 R3 {  [& o  d( I
http://127.0.0.1:5000/hello/render/str// ^3 ?. m  C. R. T/ A
结果:
3 b, [& c( h3 j; M1 K$ W$ ?2 \8 o6 ~TemplateDoesNotExist at /hello/render/str/
4 P( k: h& ]; `# a, Lindex.html
* W$ k; z6 I# E/ W) QRequest Method:        GET* c; G, H& A' M  r/ v
Request URL:        http://127.0.0.1:5000/hello/render/str/5 H1 K" `1 d+ f6 }. F5 j0 n" y
Django Version:        3.0.5
+ `7 J) w  J- w# G' M; ZException Type:        TemplateDoesNotExist8 T! N% H( ]6 Y! i
Exception Value:        - X1 z/ ?; m4 H* l7 a
index.html
! L, o& Z+ i" K$ cException Location:        C:\Users\long\AppData\Local\Programs\Python\Python38\lib\site-packages\django\template\loader.py in get_template, line 19
1 V$ e- ?  ]) |4 r/ _8 dPython Executable:        C:\Users\long\AppData\Local\Programs\Python\Python38\python.exe+ ^2 Y) G+ p2 p( R9 |- g6 R, n% R
Python Version:        3.8.8! F  t# h) N6 Y
Python Path:       
( i# v7 _0 c( |* Q4 ~6 z['C:\\Users\\long\\Desktop\\my_project',
: K% e6 F: P' Y+ e. q4 s 'C:\\Users\\long\\Desktop\\my_project',
& p4 x; G& e, \ 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',8 ~, F% u7 R9 Z: s
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',5 D- `# z( N% b0 c6 B7 {
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib',
" n* [) S  E. C; Y  X 'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38',; L* M) C% ~- L+ f" X/ i) B
'C:\\Users\\long\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']! u  R8 r" X# b
Server time:        Sun, 6 Apr 2025 11:17:52 +0000
* p7 C' A' @0 f% h0 U) L+ D
6 j8 V" _# _3 g2 I" S提示来看,是因为找不到templates的文件。
2 r$ [8 f' X9 G! q% d: C- n2 `  X. \0 q9 T/ ~& e- M
找了很久,也没有找到好的文档进行说明。最后抱着试试的态度,修改下项目目录下的setting.py中:2 |3 @4 w7 i  d1 q! ^0 m
TEMPLATES = [3 X* d7 \5 y3 e
    {
+ e1 ]) M" P9 |8 q% z        'BACKEND': 'django.template.backends.django.DjangoTemplates',7 ?4 n3 c; \1 {1 J0 p2 {0 Z
        'DIRS': ['templates'],6 ?7 d: _! E/ n! ^& u  v+ h
        'APP_DIRS': True,7 n2 S" x3 G$ g  B
        'OPTIONS': {# p/ ^- z+ J# }
            'context_processors': [
  E. d+ U$ q# Z7 A                'django.template.context_processors.debug',
# P; d* E1 ]4 A9 [                'django.template.context_processors.request',3 I; K, K7 K( S# w' ?) n
                'django.contrib.auth.context_processors.auth',6 W6 E7 I% c4 G: _) T5 @
                'django.contrib.messages.context_processors.messages',
/ q/ |, U' r) J# T$ T( r            ],4 ?# t/ n, z: h
        },( R$ D  R; P9 ~( N, m
    },* X) r# F; q& H
]0 U7 y8 W* o; s/ |5 ?6 T7 L" A
改成我们项目的一些配置:如下修改:5 n6 n' j4 M& w6 n# Z( w3 ~$ |
TEMPLATES = [" Y- K7 Y; R$ d% F3 C3 b
    {3 x" r9 v0 P' x/ r
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
' Z0 y9 @: g. s; ?        'DIRS': ['my_project/templates'],
3 n0 \# `3 }/ l. p        'APP_DIRS': True,) L% V  A1 {/ M; r) T( d
        'OPTIONS': {- p, B: o, \6 l2 Z+ P
            'context_processors': [5 ~' P* f1 b7 E
                'django.template.context_processors.debug',! W/ p5 |* ?' a
                'django.template.context_processors.request',5 v# z9 |$ O" _3 E6 |' S2 [
                'django.contrib.auth.context_processors.auth',8 w+ L7 J# w3 V+ X1 _/ O* U0 k
                'django.contrib.messages.context_processors.messages',4 ?8 Y' s3 ]/ s7 J1 b: y9 I
            ],
; L6 i, S  l8 C  k        },+ M3 u9 h0 n' `
    },( r4 S" C! J* m  Y8 ]: l
]
2 B+ G" t+ M( j! P3 J再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000" L! C% |0 R) t  P, {0 @
再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签# e+ Z: u# e3 l5 j

: K+ k, b: O  Y7 E* s问题解决。
7 R# a5 U) v) F7 ~; s/ ^0 b- [( V  J  k) B0 ]8 V# ^) j7 c
( i2 B: d9 E% ^9 J$ ?% [* v

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 19:36:32 | 显示全部楼层
有地方介绍可以使用这样的方式解决:
' ~3 {- }5 N% `8 h* B4 x修改setting.py的配置:% a0 s) F1 M4 N. H0 Q
; ]7 \$ {( k8 E6 B' _
TEMPLATES = [2 d3 b2 k6 t$ f; B: g: q9 I
    {- V8 O1 Y' Z" k  b
        'BACKEND': 'django.template.backends.django.DjangoTemplates',1 X9 k; ]4 j% a" x  v! o
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
4 \; E! `0 m- c3 w) e        'APP_DIRS': True,
% b' a8 O6 K* c3 J2 ^        'OPTIONS': {
- z2 U8 P  Z! o4 A* M, {$ U            # 其他选项...6 h2 N4 t0 F: r3 ~9 f" I
        },! h: l' ~8 g0 x9 _# @1 ~5 u
    },
1 I; t- Z4 R3 z: x  E3 c]
6 E  A$ k9 Y6 P3 \4 m2 e( `6 t- P1 N8 U1 z3 M
使用参数去获取目录:
( S) ]' D) U( A$ Z4 eos.path.join(BASE_DIR, 'templates')
8 d" y1 m! \, D6 y+ R; X6 c. X$ r+ q/ }# p
测试结果依然不能解决问题。" a0 a1 G2 F3 s) X% X
还是楼上的问题。 2 T2 M. n" M7 }5 U7 V  P
  ~: \8 ~& J3 @% m' ~
最后抱着试试的态度,修改下项目目录下的setting.py中:4 n( I% H0 E% Q3 y& x
TEMPLATES = [
' W9 g! X: O/ S    {
6 {( e; @2 ~+ p2 V( n        'BACKEND': 'django.template.backends.django.DjangoTemplates',
1 r6 K: h) t0 E; j) {        'DIRS': ['templates'],5 f, D8 L/ Q5 _. E1 T1 E- A1 F! Q7 a
        'APP_DIRS': True,# c+ ~. X5 d+ |* f
        'OPTIONS': {
% ^* T* p2 G) U0 e5 I1 ~' Y            'context_processors': [$ T# I: d1 _1 s' U0 q
                'django.template.context_processors.debug',
8 o1 v7 T" O5 J2 @, q                'django.template.context_processors.request',
- T- @/ v" P1 F/ a' ?+ N                'django.contrib.auth.context_processors.auth',
8 G7 ^' C& b7 G0 b' R( B                'django.contrib.messages.context_processors.messages',+ W: N, i5 _' X. H* Q9 W% L
            ],
1 @& s- S( F7 }9 u4 k! b$ m4 D( x        },
5 A1 k5 P1 a/ u; }# u/ Z    },: P  r5 o  l5 s/ ?+ a, ]
]
, \0 V0 W  O0 d" {9 O( E改成我们项目的一些配置:如下修改:
" f7 {& \/ ?; v- [* gTEMPLATES = [
* I; i* i/ \* |& W7 A  g8 h+ W: T; v    {
0 O& c7 Z( ?( f" C- n        'BACKEND': 'django.template.backends.django.DjangoTemplates',  e( ~6 n# F: n9 s# y, {/ g
        'DIRS': ['my_project/templates'],/ s+ [4 [( t! P) B3 a
        'APP_DIRS': True,8 }6 I% ~- v9 h
        'OPTIONS': {! w; \5 Y. o) A* T% a7 Q* `
            'context_processors': [8 x, b) K" ?/ t, c4 ~  _, S+ h
                'django.template.context_processors.debug',
4 b6 F9 @6 n% f                'django.template.context_processors.request'," U% T* y+ E5 r7 n+ _, ~
                'django.contrib.auth.context_processors.auth',% G) W  L. H9 @; k- M' V  _) C
                'django.contrib.messages.context_processors.messages',
! }* X5 o" l8 c4 z* a0 ^/ @            ],( m% q2 c8 _% B( r! o/ t* a
        },6 B' F1 t  r' ?( g& F: h0 _
    },* S5 [* c7 `0 e8 T" P
]
  I$ y8 r5 p. C8 Z再重启manage.py 服务: python manage.py runserver 0.0.0.0:5000
- _( y6 f: ~6 |0 H- s再次访问,问题解决。http://127.0.0.1:5000/hello/render/str/返回结果:我的HTML标签9 u) T. x2 G1 g1 V! w, \# t
; c, ^( Z7 k* ~2 a% N8 o  I
问题解决。
: C2 ~: P# M9 u# I4 Z1 j. c4 W  @* G1 m

5 l9 T" U; `4 I, s# b

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 19:37:19 | 显示全部楼层
使用绝对路径
5 d, F$ C( ^! q1 f# _在模板标签中,尝试使用绝对路径来加载模板,例如:
, T, h7 t5 y$ w! T( W5 W; b   
+ w, T8 K) G  ]/ T3 U, z2 r; E) u{% extends 'app_name/template_name.html' %}
/ r9 Z5 |0 x) r( e确保替换app_name和template_name.html为正确的值。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 20:55:49 | 显示全部楼层
后来测试,还发现一个问题,这个地方配置其实是没有问题的,
3 E! x. h1 V1 G- vTEMPLATES = [
# [! T8 i/ _# y  `: E6 m    {
7 J0 G4 ?9 q7 ?3 t& ^. z        'BACKEND': 'django.template.backends.django.DjangoTemplates',3 Z2 z& }! @! N+ G0 r. O: e5 g0 O2 j, t
        'DIRS': [os.path.join(BASE_DIR, 'templates')],4 X! Z' L  `  G+ ^3 x/ j# ?" o
        'APP_DIRS': True,
+ ^$ G# f: l7 P        'OPTIONS': {
/ n0 Y9 }, l8 V" t  i# {% f            # 其他选项...
* e8 ~% `# l+ j) h% n        },% p6 `  K* Q/ I0 h/ F
    },
2 K" ?' d5 i5 G$ b! ]5 o]0 G6 ?: X6 H2 [8 ~1 c9 ~( D
6 j) d9 [! W7 }% w+ y
而是忽略了一个问题,需要把templates这个文件夹放在大根目录下。
" U# j- Z; ]9 `3 O

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-6 20:57:36 | 显示全部楼层
这样写:
9 I0 N4 M" @# n7 ^: z) b'DIRS': [os.path.join(BASE_DIR, 'templates')],
' S9 ], z. z4 b9 z或者这样写都可以
+ q) Z* {; Z: ~9 i  'DIRS': ['templates'],! b9 b0 M- k$ r7 Z
/ p. A- V' [# A* m5 U/ p5 d6 Z9 j1 |
会报错是因为没有注意到是否在根目录下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-12 00:10 , Processed in 0.017575 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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