0
votes

Can anyone help me please. I get this error:

TemplateDoesNotExist at / .html

Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 3.0.4 Exception Type: TemplateDoesNotExist Exception Value: .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']

Traceback

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, "index.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: .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),
]
1

1 Answers

0
votes

I solved it. I changed

return render(request, "index.html", {'form': context})

to this

return render(request, "index.html", context)