python - TemplateDoesNotExist at / 模板路径
问题描述
我想创建投资组合的后端,我正在尝试使用模板,但它说它不存在,但在错误消息中显示了正确的文件路径。这是 urls.py:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('core.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
这是我的views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
def home(request):
template = loader.get_template('home.html')
return HttpResponse(template.render(request))
这是我的模板设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
....
],
},
},
]
目录结构为:
核心是应用名称
解决方案
试试这个
将主视图更改为
def home(request):
return render("home.html")
并在 settings.py 中将 DIRS 设置为空白
TEMPLATES = [
{
...
'DIRS': [],
...
}
]
推荐阅读
- javascript - svg 动画圆形旋钮顺时针旋转不使用 GSAP
- flutter - 如何在不使用边界半径的情况下创建弯曲的小部件
- javascript - DSP.js-幅度改变频率等等
- php - 如何将参数传递到链式 Laravel Eloquent 关系中的位置
- javascript - 检测任何可能打开链接而不知道是哪个元素的点击
- php - 如何通过php检查目录中丢失的文件
- java - Maven 中工件的访问限制(模块化)
- java - 如何使用 Spring Security 将对象从 AJAX 传递到 Spring Controller
- python - 试图读取一个大的 csv 文件并且连接速度太慢
- joomla3.0 - Joomla 自定义模块中的布局