gpt4 book ai didi

python - django TemplateDoesNotExist 异常但模板存在

转载 作者:行者123 更新时间:2023-12-01 02:06:25 34 4
gpt4 key购买 nike

我正在尝试在 django 中渲染模板。当我连接到该网站时出现此错误:

django.template.loaders.filesystem.Loader: path-to-azerty/templates/base.html (Source does not exist)

这是我的项目目录结构:

azerty/  
__init__.py
├── settings.py
├── templates
│   └── base.html
├── urls.py
├── views.py
└── wsgi.py

这是我的代码:

// ulrs.py

from django.contrib import admin
from django.urls import path
from azerty import views

urlpatterns = [
path('', views.index),
path('admin/', admin.site.urls),
]



// settings.py

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]



//views.py

from django.shortcuts import render

def welcome(request):
return render(request, 'base.html')`

最佳答案

您的“DIRS”设置适用于项目目录中的模板目录(包含 manage.py 的目录)。

如果您希望模板位于内部目录(包含settings.py的目录),那么您需要将其更改为:

os.path.join(BASE_DIR, 'azerty', 'templates')],

有时使用的另一个选项是将azerty 添加到您的INSTALLED_APPS。然后应用程序加载器将找到该目录,您可以使用 'DIRS': [],

关于python - django TemplateDoesNotExist 异常但模板存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49017297/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com