【发布时间】:2018-12-27 07:26:50
【问题描述】:
尝试使用 Django 2.1.4 创建一个简单的应用程序。
项目名称为“inventory_management”,项目内的应用程序名为“gInventory”。
这是我的文件管理的树视图:
| 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
每当我运行本地服务器并尝试打开网页时,我都希望 index.html 出现。相反,我收到一条错误消息,指出“TemplateDoesNotExist at / index.html”——该模板不存在,但我相信该路径是有效的。
该错误下方是 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 -- 库存管理
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/'
最后,views.py -- gInventory
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'index.html')
感谢任何可以提供帮助的人!
【问题讨论】:
-
inventoryvsgInventoryinINSTALLED_APPS。