任何人都可以帮助我。 我收到此错误:
TemplateDoesNotExist位于/ .html
请求方法:GET请求 URL: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:\ Users \ Davids' '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:\ Users \ Davids' 'dator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages']
跟踪
回溯(最近一次通话最后一次):文件“ C:\ Users \ Davids dator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ django \ core \ handlers \ exception.py”, 第34行,在内部 response = get_response(request)文件“ C:\ Users \ Davids dator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ django \ core \ handlers \ base.py”, 第115行,在_get_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“, _get_response中的第113行 响应= wraped_callback(请求,* callback_args,** callback_kwargs)文件“ C:\ Users \ Davids dator \ Desktop \ templateee \ MySite \ MyApp \ views.py”,索引中的第36行 返回render(request,“ index.html”,{'form':context})文件“ C:\ Users \ Davids dator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ django \ shortcuts.py“, 第19行,在渲染中 内容= loader.render_to_string(模板名称,上下文,请求,使用=使用)文件“ C:\ Users \ Davids dator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ django \ template \ loader.py“, 第61行,在render_to_string中 template = 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中 引发TemplateDoesNotExist(template_name,chain = chain)
异常类型:TemplateDoesNotExist at /异常值:.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, "index.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'),
)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'MyApp',
]
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',
]
ROOT_URLCONF = 'MySite.urls'
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',
],
},
},
]
WSGI_APPLICATION = 'MySite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Urls.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),
]
答案 0 :(得分:0)
我解决了。我改变了
return render(request, "index.html", {'form': context})
对此
return render(request, "index.html", context)