0
votes

I created new project and can't find where the place where mistake was made.
Django versiob - 3.1.5
Python 3.7.4

TemplateDoesNotExist at / index.html Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 3.1.5 Exception Type: TemplateDoesNotExist Exception Value:
index.html Exception Location: C:\Users\user\PycharmProjects\Django learning\Portfolio\venv\lib\site-packages\django\template\loader.py, line 19, in get_template Python Executable: C:\Users\user\PycharmProjects\Django learning\Portfolio\venv\Scripts\python.exe Python Version: 3.7.4 Python Path:
['C:\Users\user\PycharmProjects\Django learning\Portfolio\landingpage', 'C:\Users\user\AppData\Local\Programs\Python\Python37\python37.zip', 'C:\Users\user\AppData\Local\Programs\Python\Python37\DLLs', 'C:\Users\user\AppData\Local\Programs\Python\Python37\lib', 'C:\Users\user\AppData\Local\Programs\Python\Python37', 'C:\Users\user\PycharmProjects\Django learning\Portfolio\venv', 'C:\Users\user\PycharmProjects\Django ' 'learning\Portfolio\venv\lib\site-packages', 'C:\Users\user\PycharmProjects\Django ' 'learning\Portfolio\venv\lib\site-packages\setuptools-40.8.0-py3.7.egg', 'C:\Users\user\PycharmProjects\Django ' 'learning\Portfolio\venv\lib\site-packages\pip-19.0.3-py3.7.egg']

structure:

landingpage:
  --forms #app:
      urls
      views
  --landingpage:
      templates:
        index.html
      settings.py
      urls
      others ...

landingpage/settings:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
project_name = "landingpage"
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*'

# 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',
    # apps
    'forms',
]

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 = 'landingpage.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',
            ],
        },
    },
]

landingpage/urls:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('forms.urls')),
]

forms/urls:

from django.urls import path
from . import views

app_name = 'forms'

urlpatterns = [
    path('', views.index, name='index')
]

views:

from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'index.html')

landingpage/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>

enter image description here

1

1 Answers

0
votes

Don't put your templates in landingpage folder. You should put your Templates in your app forms

Try to use this :-

Make a new folder in forms named templates and then make a new folder in templates named forms.

Your structure of folders should be like this :-

forms -> templates -> forms -> index.html

Use this in your views.py :-

def index(request):
    return render(request, 'forms/index.html')