在 / 获取 TemplateDoesNotExist

Getting TemplateDoesNotExist at /

我遇到错误

TemplateDoesNotExist at /AI.html

我试图寻找问题,但找不到。

Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 3.0.4 Exception Type: TemplateDoesNotExist Exception Value:
AI.html Exception Location: C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py in get_template, line 19 Python Executable: C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\python.exe Python Version: 3.7.6 Python Path:
['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']

追溯

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/

Django Version: 3.0.4 Python Version: 3.7.6 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'MyApp'] 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.app_directories.Loader: C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\admin\templates\AI.html (Source does not exist) * django.template.loaders.app_directories.Loader: C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\templates\AI.html (Source does not exist)

Traceback (most recent call last): File "C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\Davids dator\Desktop\templateee\Mysite\MyApp\views.py", line 36, in Index return render(request, "AI.html", {'form': context}) File "C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\shortcuts.py", line 19, in render 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", line 61, in render_to_string template = get_template(template_name, using=using) File "C:\Users\Davids dator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py", line 19, in get_template raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at / Exception Value: 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',
            ],
        },
    },
]

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),
]

您的 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", ], }, }, ]