2015-07-14 21 views
7

amici. Provo a ripetere facendo l'esempio del progetto nel libro: "Learning Django Web Development" di Jaiswal, Sanjeev.TemplateDoesNotExist at/base.html

esecuzione del server ottengo tale eccezione: TemplateDoesNotExist a /base.html

TemplateDoesNotExist at/
base.html 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/ 
Django Version:  1.8.3 
Exception Type:  TemplateDoesNotExist 
Exception Value: base.html 

Exception Location: C:\Python34\lib\site-packages\django\template\loader.py in get_template, line 46 
Python Executable: C:\Python34\python.EXE 
Python Version:  3.4.3 
Python Path:  

['C:\\dj\\mytweets', 
'C:\\WINDOWS\\system32\\python34.zip', 
'C:\\Python34\\DLLs', 
'C:\\Python34\\lib', 
'C:\\Python34', 
'C:\\Python34\\lib\\site-packages'] 

Server time: Tue, 14 Jul 2015 14:01:27 +0300 

Template-loader postmortem

Django tried loading these templates, in this order: 

    Using loader django.template.loaders.filesystem.Loader: 
    Using loader django.template.loaders.app_directories.Loader: 
     C:\Python34\lib\site-packages\django\contrib\admin\templates\base.html (File does not exist) 
     C:\Python34\lib\site-packages\django\contrib\auth\templates\base.html (File does not exist) 

Il mio file settings.py:

import os 

SETTINGS_PATH = os.path.dirname(__file__) 
PROJECT_PATH = os.path.join(SETTINGS_PATH, os.pardir) 
PROJECT_PATH = os.path.abspath(PROJECT_PATH) 
TEMPLATE_PATH = os.path.join(PROJECT_PATH, "templates") 

SECRET_KEY = 'khcr3h6u+ghi+rtb+g_(mvgq!mtn9u4&%=hu20vt2*u(p8-kde' 

DEBUG = True 
TEMPLATE_DEBUG = True 
ALLOWED_HOSTS = [] 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'tweets', 
) 

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 = 'mytweets.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(PROJECT_PATH, 'templates')], 
     '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 = 'mytweets.wsgi.application' 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(PROJECT_PATH, 'db.sqlite3'), 
    } 
} 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 

STATIC_URL = '/static/' 
STATICFILES_DIRS = (
    os.path.join(
     os.path.dirname(__file__), 
     'static', 
    ), 
) 

TEMPLATE_DIRS = (
    TEMPLATE_PATH, 
) 

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
) 

I provato a cambiare settings.py in questo modo:

mutato settings.py:

import os 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
TEMPLATE_PATH = os.path.join(BASE_DIR, "templates") 

SECRET_KEY = 'khcr3h6u+ghi+rtb+g_(mvgq!mtn9u4&%=hu20vt2*u(p8-kde' 

DEBUG = True 
TEMPLATE_DEBUG = True 
ALLOWED_HOSTS = [] 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'tweets', 
) 

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 = 'mytweets.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'templates')], 
     '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 = 'mytweets.wsgi.application' 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


STATIC_URL = '/static/' 
STATICFILES_DIRS = (
    os.path.join(
     os.path.dirname(__file__), 
     'static', 
    ), 
) 

La mia struttura del progetto:

My project structure:

views.py:

from django.views.generic import View 
from django.shortcuts import render 


class Index(View): 
    def get(self, request): 
     params = {} 
     params['name'] = 'Django' 
     return render(request, 'base.html', params) 

urls.py:

from django.conf.urls import patterns, include, url 
from django.contrib import admin 
from tweets.views import Index 
admin.autodiscover() 

urlpatterns = patterns('', 
    url(r'^$', Index.as_view()), 
    url(r'^admin/', include(admin.site.urls)), 
) 

Traceback:

Template Loader Error: 
Django tried loading these templates, in this order: 
Using loader django.template.loaders.filesystem.Loader: 
Using loader django.template.loaders.app_directories.Loader: 
C:\Python34\lib\site-packages\django\contrib\admin\templates\base.html (File does not exist) 
C:\Python34\lib\site-packages\django\contrib\auth\templates\base.html (File does not exist) 



Traceback: 
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response 
132.response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "C:\Python34\lib\site-packages\django\views\generic\base.py" in view 
71.return self.dispatch(request, *args, **kwargs) 
File "C:\Python34\lib\site-packages\django\views\generic\base.py" in dispatch 
89.return handler(request, *args, **kwargs) 
File "C:\dj\mytweets\tweets\views.py" in get 
9.return render(request, 'base.html', params) 
File "C:\Python34\lib\site-packages\django\shortcuts.py" in render 
67.template_name, context, request=request, using=using) 
File "C:\Python34\lib\site-packages\django\template\loader.py" in render_to_string 
98.template = get_template(template_name, using=using) 
File "C:\Python34\lib\site-packages\django\template\loader.py" in get_template 
46.raise TemplateDoesNotExist(template_name) 

Exception Type: TemplateDoesNotExist at/
Exception Value: base.html 

prega, dare un consiglio, che cosa devo cambiare per ottenere page reso?

+0

Puoi condividere anche mytweets/urls.py e tweets/views.py. –

+0

Sì, certo. Dare un secondo – ufo

risposta

5

Non ho familiarità con il libro che stai utilizzando, quindi non posso darti alcun consiglio basato su quello. Se il libro è per Django 1.7, sarà più facile usare Django 1.7 invece di Django 1.8, almeno quando inizi con Django.

Se si vuole attaccare con Django 1.8, ecco come risolvere l'errore che si sta vedendo:

Il file settings.py ha una miscela di vecchie impostazioni modelli, come TEMPLATE_DIRS e TEMPLATE_LOADERS (Django < = 1.7), e le nuove impostazioni sotto TEMPLATES (Django 1.8+).

Innanzitutto, rimuovere le vecchie impostazioni TEMPLATE_DIRS e TEMPLATE_LOADERS.

In secondo luogo, sembra che DIRS non sia corretto nell'impostazione TEMPLATES.

Definire BASE_DIR, che dovrebbe essere incluso nel settings.py per impostazione predefinita quando si esegue ./manage.py startproject

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 

Poi cambiare TEMPLATES a

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'templates')], 
     ... 
+0

Ciao, grazie per la mancia. Ho cambiato tutte le impostazioni consigliate, ma non aiuta – ufo

+0

Cosa viene visualizzato quando aggiungi la stampa (TEMPLATES) al tuo 'settings.py'? Per favore [modifica] la tua domanda e includi il nuovo traceback. Copia e incolla il testo invece di includere uno screenshot. – Alasdair

+0

stampa (TEMPLATES) visualizza tale elenco: [{'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 ']},' APP_DIRS ': True,' DIRS ': [' C: \\ Documents and Settings \\ Admin \\ Мои документы \\ dj \\ mytweets \ \ templates '],' BACKEND ':' django.template.backends.django.DjangoTemplates '}] – ufo

-1

Impostare il percorso del modello in DIR [] di impostazioni.py

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [**'C:\\Python27\\Lib\\site-packages\\django\\contrib\\admin\\templates\\admin'**], 
     'APP_DIRS':True, 
     'OPTIONS': {