运行python manage.py runserver
时出现错误“ /home_page.html处的TemplateDoesNotExist”。我的文件夹结构包含在下面的屏幕截图中。运行服务器时如何显示模板?
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',
],
},
},
]
This is the Traceback:
Internal Server Error: /
Traceback (most recent call last):
File
"/Users/melissamalala/PycharmProjects/ecommerce/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/melissamalala/PycharmProjects/ecommerce/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/melissamalala/PycharmProjects/ecommerce/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/melissamalala/PycharmProjects/ecommerce/ecommerceprojects/views.py", line 17, in home_page
return render(request, "home_page.html", context)
File "/Users/melissamalala/PycharmProjects/ecommerce/venv/lib/python3.6/site-packages/django/shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/melissamalala/PycharmProjects/ecommerce/venv/lib/python3.6/site-packages/django/template/loader.py", line 67, in render_to_string
template = get_template(template_name, using=using)
File "/Users/melissamalala/PycharmProjects/ecommerce/venv/lib/python3.6/site-packages/django/template/loader.py", line 25, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: home_page.html
[25/Oct/2018 19:27:32] "GET / HTTP/1.1" 500 89057
答案 0 :(得分:0)
在模板文件夹中,创建一个具有您应用名称的文件夹,并将home_page.html放在其中。
所以结构看起来像这样:
Project_dir
app_dir
templates
app_dir
home_page.html
答案 1 :(得分:0)
您是否尝试过显式设置模板加载器?
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader'
]
对我有用