在 / 处获取 TemplateDoesNotExist
Posted
技术标签:
【中文标题】在 / 处获取 TemplateDoesNotExist【英文标题】:Getting TemplateDoesNotExist at / 【发布时间】:2020-06-20 01:56:30 【问题描述】:我遇到错误
TemplateDoesNotExist at /AI.html
我试图寻找问题,但我找不到它。
请求方法:GET 请求网址:http://127.0.0.1:8000/ Django 版本:3.0.4 异常类型:TemplateDoesNotExist 异常值: 人工智能.html 异常位置:C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py 在 get_template,第 19 行 Python 可执行文件:C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\python.exe Python版本:3.7.6 Python 路径: ['C:\Users\Davids dator\Desktop\templateee\Mysite', 'C:\用户\大卫' 'dator\AppData\Local\Programs\Python\Python37\python37.zip', 'C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\DLLs', 'C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib', 'C:\Users\Davids dator\AppData\Local\Programs\Python\Python37', 'C:\Users\Davids dator\AppData\Roaming\Python\Python37\site-packages', 'C:\用户\大卫' 'dator\AppData\Local\Programs\Python\Python37\lib\site-packages']
追溯
环境:
请求方法:GET 请求地址:http://127.0.0.1:8000/
Django 版本:3.0.4 Python 版本:3.7.6 已安装的应用程序: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'MyApp'] 已安装的中间件: ['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']
模板加载器事后 Django 尝试加载这些模板,在 这个顺序:
使用引擎 django: * django.template.loaders.app_directories.Loader: C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\admin\templates\AI.html (来源不存在) * django.template.loaders.app_directories.Loader: C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\templates\AI.html (来源不存在)
Traceback(最近一次调用最后一次):文件“C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", 第 34 行,在内部 response = get_response(request) File "C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", 第 115 行,在 _get_response 中 response = self.process_exception_by_middleware(e, request) 文件“C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", 第 113 行,在 _get_response 中 response = Wrapped_callback(request, *callback_args, **callback_kwargs) 文件“C:\Users\Davids dator\Desktop\templateee\Mysite\MyApp\views.py”,第 36 行,在索引中 返回渲染(请求,“AI.html”,'form': context)文件“C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\shortcuts.py", 第 19 行,在渲染中 content = loader.render_to_string(template_name, context, request, using=using) File "C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py", 第 61 行,在 render_to_string 中 模板 = get_template(template_name, using=using) 文件“C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py", 第 19 行,在 get_template 中 raise TemplateDoesNotExist(template_name, chain=chain)
异常类型:TemplateDoesNotExist at / 异常值:AI.html
Views.py
from django.shortcuts import render
from django.http import HttpResponse
import json
from django.views.decorators.csrf import csrf_exempt
from chatterbot import ChatBot
# Create your views here.
chatbot = ChatBot(
'Ron Obvious',
trainer='chatterbot.trainers.ChatterBotCorpusTrainer'
)
@csrf_exempt
def get_response(request):
response = 'status': None
if request.method == 'POST':
data = json.loads(request.body.decode('utf-8'))
message = data['message']
chat_response = chatbot.get_response(message).text
response['message'] = 'text': chat_response, 'user': False, 'chat_bot': True
response['status'] = 'ok'
else:
response['error'] = 'no post data found'
return HttpResponse(
json.dumps(response),
content_type="application/json"
)
def Index (request):
context = 'title': 'Chatbot Version 1.0'
return render(request, "AI.html", 'form': context)
Settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
project_root = os.path.abspath(os.path.dirname(__file__))
STATIC_DIRS = (
os.path.join(project_root, 'static'),
)
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
,
,
]
网址.py
from django.contrib import admin
from django.conf.urls import include, url
from django.urls import path
from MyApp.views import Index, get_response
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
url('', Index),
path('get-response/', get_response),
]
【问题讨论】:
【参考方案1】:您的settings.py
缺少模板目录。
你应该添加
TEMPLATES = [ "BACKEND": "django.template.backends.django.DjangoTemplates", "DIRS": ["/absolute/path/to/your/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", ], , , ]
【讨论】:
感谢您的提醒,但实际上我已经有了。我已经更新了 settings.py'DIRS': [],
是空列表吗?如果是这样,您应该将绝对路径添加到您的模板目录。
是的...没有意识到这一点。谢谢!我会尝试添加它,看看它是否效果更好以上是关于在 / 处获取 TemplateDoesNotExist的主要内容,如果未能解决你的问题,请参考以下文章
如何在 NSURLSessionDownloadTask 中的 UIApplicationWillTerminateNotification 处获取 resumeData