[英]TemplateDoesNotExist Error in django on a simple hello world project
我在 Django 中使用模板時遇到問題。 我相信我在正確的位置有模板,在查看錯誤日志中的路徑后,文件就在那里。 我也讓它在不使用render()
情況下工作。 但是我嘗試了多種方法,並且在錯誤日志中顯示了一個路徑,我可以通過該路徑訪問要呈現的 html 文件。
下面是我項目的文件結構(注意:這個文件結構其實在C:\\my_website里面)
相關代碼:
以下是我創建的 hello 應用程序的視圖。 當前情況如何,但如果我注釋掉render()
調用並使用未注釋的代碼,它將顯示 index.html 的內容
C:\\my_website\\my_website\\hello\\views
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
import os
import logging #MS ADDED
logger = logging.getLogger(__name__)#MS ADDED
def index(request):
#!!! It works with this stuff uncommented
#index_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),'hello\\templates\\hello\\index.html')
#logger.error('Views - index_path is ' + index_path) # MS ADDED
#with open(index_path) as f:
# html_string = f.read()
#return HttpResponse(html_string)
return render(request, 'hello/index.hmtl')
以下是我在設置中更改的所有內容。 我在 Django 文檔中讀到,如果您沒有指定路徑,它將在您創建的應用程序中的模板目錄中查找(你好)。 但是在 TEMPLATES 中將 DIRS 留空確實會導致 TemplateDoesNotExist 異常。 所以我嘗試了其他一些不起作用的東西,如下所示。
C:\\my_website\\my_website\\my_website\\settings
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello.apps.HelloConfig',
]
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))#MS ADDED
Temp_Path = os.path.realpath('.')# MS ADDED
index_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),'hello/templates')#MS ADDED
#MS Note: I tried for DIRS below os.path.join(SETTINGS_PATH,'templates')
#MS Note: I tried for DIRS below Temp_Path + "hello/templates"
#MS Note: I tried for DIRS Below os.path.join(os.path.dirname(os.path.dirname(__file__)),'hello/templates')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [index_path],
'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',
],
},
},
]
下面是網址
C:\\my_website\\my_website\\my_website
from django.contrib import admin
from django.urls import path
from hello import views
urlpatterns = [
path('',views.index,name='index'),
path('admin/', admin.site.urls),
#path('hello/', hello.views.index),
]
錯誤日志:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.1.2
Python Version: 3.8.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello.apps.HelloConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: C:\my_website\my_website\templates\hello\index.hmtl (Source does not exist)
* django.template.loaders.app_directories.Loader: C:\my_website\env\lib\site-packages\django\contrib\admin\templates\hello\index.hmtl (Source does not exist)
* django.template.loaders.app_directories.Loader: C:\my_website\env\lib\site-packages\django\contrib\auth\templates\hello\index.hmtl (Source does not exist)
* django.template.loaders.app_directories.Loader: C:\my_website\my_website\hello\templates\hello\index.hmtl (Source does not exist)
Traceback (most recent call last):
File "C:\my_website\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\my_website\env\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\my_website\my_website\hello\views.py", line 16, in index
return render(request, 'hello/index.hmtl')
File "C:\my_website\env\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\my_website\env\lib\site-packages\django\template\loader.py", line 61, in render_to_string
template = get_template(template_name, using=using)
File "C:\my_website\env\lib\site-packages\django\template\loader.py", line 19, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
Exception Type: TemplateDoesNotExist at /
Exception Value: hello/index.hmtl
在錯誤日志中它說這個
C:\\my_website\\my_website\\hello\\templates\\hello\\index.hmtl (Source does not exist)
但它確實存在並且它只包含<h1>Hello World! aafa</h1>
<h1>Hello World! aafa</h1>
使用視圖中注釋掉的代碼,應用程序顯示:
我正在使用 django 3.1.2 版和 python 3.8.6 版
任何幫助將不勝感激
在渲染函數中,您傳遞的是“index.hmtl”而不是“index.html”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.