我正在尝试呈现一个简单的页面,但我遇到了一个问题。
TemplateDoesNotExist at /pages/
{}
找不到模板文件夹。这是我的 settings.py 配置
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'pages/template')],
'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',
],
},
},
]
我做错了什么吗?
-------------- 命令指令 ————–
Internal Server Error: /pages/
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\Scripts\src\pages\views.py", line 6, in index
return render('index.html', context)
File "C:\Python34\lib\site-packages\django\shortcuts.py", line 67, in render
template_name, context, request=request, using=using)
File "C:\Python34\lib\site-packages\django\template\loader.py", line 96, in render_to_string
template = get_template(template_name, using=using)
File "C:\Python34\lib\site-packages\django\template\loader.py", line 43, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: {}
[12/Jan/2016 20:51:57] "GET /pages/ HTTP/1.1" 500 74646
- - - - - - 更新 - - - - - - -
文件树
视图.py
from django.shortcuts import render
# Create your views here.
def index(request):
context = {}
return render(request, 'index.html', context)
原文由 Hiroyuki Nuri 发布,翻译遵循 CC BY-SA 4.0 许可协议
当您调用 --- 时,您忘记了第一个参数
request
render
。您可以有多个模板目录,例如
src/template
和pages/template
。如果你想要一个src/template
目录,那么你需要将它包含在你的DIRS
选项中。您不需要在
pages/templates
DIRS
目录中的 --- 应用程序加载器将找到该目录中的模板,因为您有APP_DIRS
True
和pages
在你的INSTALLED_APPS
设置中。