我正在尝试使用着陆页制作基本的Django应用程序。我做了以下事情:
python manage.py startapp landing
然后我将landing
添加到INSTALLED_APPS
,将url(r'^$', 'landing.views.index')
添加到urlpatterns
。
在landing/view.py
内,我补充道:
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
def index(request):
return render_to_response('landing/index.html', context_instance=RequestContext(request))
landing/index.html
是我想要的目标网页。
当我尝试访问着陆页时,我收到错误:
TemplateDoesNotExist at /
landing/index.html
我做错了什么?
settings.py
的设置如下:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'landing',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'learningSuite.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 = 'learningSuite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.9/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/1.9/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',
},
]
答案 0 :(得分:2)
模板需要进入单独的模板目录。它们都应该位于DIRS设置中专门命名的目录中,或者如果APP_DIRS为True,则它们可以位于<app_name>/templates
内。
答案 1 :(得分:1)
您应该将APP_DIRS
设置为True
,以允许Django在项目应用的templates
目录中查找模板。
并且,您需要将index.html
放在myProject/landing/templates/
目录中。