我是DJANGO的初学者..我正在创建我的第一个模板并尝试通过get_template()加载到我的视图中。但它显示错误“TemplateDoesNotExist at / time /”。我不知道我做错了什么。这些是我的档案。
------------ ----------- settings.py
import os
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/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'cg@&q^y)&nvn=te*h!)ax#t4@=_t#phjr_4cr)+8xs$s7iwtir3'
# 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',
)
MIDDLEWARE_CLASSES = (
'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',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'pr1.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 = 'pr1.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/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/1.8/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_DIRS=[
'/home/sidharth/Desktop/projects/project1/pr1/pr1/templates'
]
----------- urls.py -------------
from django.conf.urls import patterns, include, url
from django.contrib import admin
from views import current_date_time
from views import hours_ahead
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^time/$',current_date_time),
(r'^time/(\d)/$', hours_ahead ),
)
------------ ----------- views.py
from django.shortcuts import render
from django.template import Template, Context
from django.template.loader import get_template
# Create your views here.
from django.http import HttpResponse
import datetime
def current_date_time(response):
now=datetime.datetime.now()
t=get_template('time.html')
time=t.render(Context({'time':now}))
return HttpResponse(time)
def hours_ahead(response,offset):
offset=int(offset)
final_time=datetime.datetime.now()+ datetime.timedelta(hours=offset)
final="<html><body>the time after %s hours will be %s</body></html>" %(offset,final_time)
return HttpResponse(final)
---------------为time.html ------------
<html><body>It is now {{ time }}.</body></html>
答案 0 :(得分:2)
正如您在official release django docs中所看到的,在版本1.8中,有关模板参数的配置方式已发生变化。
TEMPLATE_DIRS
已被弃用,因此您可以将其从settings.py
中删除。
相反,在TEMPLATES
中,正确设置DIRS
键(现在为空)。
例如:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'), ], # check the path depending on your prj structure
'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',
],
},
},
]