2015-10-04 102 views
1

即時消息在DJANGO ..我創建我的第一個模板,並試圖通過get_template()加載到我的視圖。但是它顯示了一個錯誤「TemplateDoesNotExist at/time /」。我不知道我做錯了什麼。這些是我的文件。Django,無法通過TEMPLATE_DIRS加載模板

------------ ----------- 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 = '[email protected]&q^y)&nvn=te*h!)ax#[email protected]=_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' 
] 

-----------網址。 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> 

回答

2

正如您在official release django docs中看到的那樣,在版本1.8中,有關如何配置模板參數的事情已經發生了變化。您可以從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', 
      ], 
     }, 
    }, 
]