Attempting to create a simple application using Django 2.1.4.
Project name is "inventory_management", application inside the project is called "gInventory".
Here is tree view of my file management:
| db.sqlite3
| manage.py
| tree.txt
|
+---gInventory
| | admin.py
| | apps.py
| | models.py
| | tests.py
| | urls.py
| | views.py
| | __init__.py
| |
| +---migrations
| | __init__.py
| |
| +---static
| | +---css
| | | bootstrap-grid.css
| | | bootstrap-grid.css.map
| | | bootstrap-grid.min.css
| | | bootstrap-grid.min.css.map
| | | bootstrap-reboot.css
| | | bootstrap-reboot.css.map
| | | bootstrap-reboot.min.css
| | | bootstrap-reboot.min.css.map
| | | bootstrap.css
| | | bootstrap.css.map
| | | bootstrap.min.css
| | | bootstrap.min.css.map
| | | style.css
| | |
| | \---js
| | bootstrap.bundle.js
| | bootstrap.bundle.js.map
| | bootstrap.bundle.min.js
| | bootstrap.bundle.min.js.map
| | bootstrap.js
| | bootstrap.js.map
| | bootstrap.min.js
| | bootstrap.min.js.map
| |
| +---templates
| | base.html
| | index.html
| |
| \---__pycache__
| urls.cpython-37.pyc
| views.cpython-37.pyc
| __init__.cpython-37.pyc
|
\---inventory_management
| settings.py
| urls.py
| wsgi.py
| __init__.py
|
\---__pycache__
settings.cpython-37.pyc
urls.cpython-37.pyc
wsgi.cpython-37.pyc
__init__.cpython-37.pyc
Whenever I run my local server and try and pull up the webpage, I'm expecting index.html to show up. Instead, I'm getting an error stating "TemplateDoesNotExist at / index.html" - the template doesn't exist, however I believe that the path is valid.
Below that error is the attempts to load by Django:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\ateox\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\ateox\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)
urls.py -- inventory_management:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [ #define urls from apps
path('admin/', admin.site.urls),
path('', include('gInventory.urls'))
]
urls.py -- gInventory:
from django.conf.urls import url
from .views import index
urlpatterns =[
url(r'^$', index, name = "index"),
]
settings.py -- inventory_management
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__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'thkot@lru9#uo1ub$wpa%u!+)t3lcee^0#f*h8c=imvz@^lq8c'
# 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',
'inventory',
]
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 = 'inventory_management.urls'
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',
],
},
},
]
WSGI_APPLICATION = 'inventory_management.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
and lastly, views.py -- gInventory
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'index.html')
Thanks to anyone who can help!
In your Settings.py
, you need to put replace DIRS
in your TEMPLATES
with this code.
'DIRS': [os.path.join(BASE_DIR, 'templates')],
This defines you have your template
folder at base directory of project.
Convention that you should follow:
Create a folder named after your app inside templates
folder and put your html inside it.
+---templates/ | ginventory/ | base.html | index.html
In your view
functions you give html address as:
def index(request): return render(request, 'ginventory/index.html')
Move your template folder under gInventory
directory
If you want to do it as official documentations suggested, follow these steps:
Your project's TEMPLATES setting describes how Django will load and render templates. The default settings file configures a DjangoTemplates backend whose APP_DIRS option is set to True. By convention DjangoTemplates looks for a “templates” subdirectory in each of the INSTALLED_APPS.
So check if your app name is present inside INSTALLED_APPS
list. Then check inside settings.py
and look for TEMPLATES
then check if 'APP_DIRS': True
.
Your template file should be in this path: myproject/myapp/templates/myapp/mypage.html
This is the way django looks for templates in every app.
Finally your views function should end with something like this:
def index(request):
....
return render(request, 'portal/index.html', context)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.