Python TemplateDoesNotExist位于/base.html

Python TemplateDoesNotExist位于/base.html,python,django,Python,Django,朋友们。 我试图通过在Sanjeev Jaiswal的《学习Django Web开发》一书中的项目示例来重复 运行服务器时会出现这样的异常:TemplateDoesNotExist at/base.html TemplateDoesNotExist at / base.html Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 1.8.3 Exception Type:

朋友们。 我试图通过在Sanjeev Jaiswal的《学习Django Web开发》一书中的项目示例来重复

运行服务器时会出现这样的异常:TemplateDoesNotExist at/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
模板加载程序后期处理

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)
My 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',
)
我也尝试以这种方式更改settings.py:

已更改的设置.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',
    ),
)
我的项目结构:

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)
URL.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)),
)
回溯:

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

请给出一个建议,我应该更改什么才能获得渲染页面?

我对您正在使用的书不熟悉,因此我不能基于此给您任何建议。如果这本书是针对Django 1.7的,您会发现使用Django 1.7而不是Django 1.8会更容易,至少在您刚开始使用Django时是这样

如果您想继续使用Django 1.8,以下是如何修复您当前看到的错误:


您的
settings.py
文件混合了旧模板设置,如
TEMPLATE\u DIRS
TEMPLATE\u LOADERS
(Django在settings.py的DIR[]中设置模板路径)

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

我也遇到了同样的问题,首先我试图通过将HTML模板的快捷方式路径添加到
{%extends“base.HTML”%}
{%extends”foldername/template/base.HTML“%}
来解决这个问题,但它不起作用


之后,我将其添加到每个项目模板文件夹中的“base.html”文件中。这对我很有效。

在已安装的应用程序中,添加mytweets而不是tweets。

我正是遇到了这个问题,在我的项目/templates/base.html中找不到base.html

我修复了在调用它的应用程序之后移动目录模板的问题 (马车前)


你能不能也分享mytweets/URL.py和tweets/views.py。是的,当然。给一个secHi,thanx作为提示。我更改了你建议的所有设置,但这无助于你添加打印(模板)时显示什么到您的
设置.py
?请回答您的问题并包括新的回溯。复制并粘贴文本,而不是包括屏幕截图。打印(模板)显示这样的列表:[{'OPTIONS':{'context\u 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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,'BACKEND':'django.template.backends.django.DjangoTemplates'}]既然您已经更改了设置,那么回溯又如何呢?我在设置中添加了有问题的回溯的postnever do硬编码路径-上面的代码有两个主要问题-不仅是硬编码+如此多的人甚至没有C驱动器,更不用说将其部署到生产环境中将永远无法工作-但无论如何,硬编码路径a我们反对正确的软件工程和编码惯例。。。
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
 TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [**'C:\\Python27\\Lib\\site-packages\\django\\contrib\\admin\\templates\\admin'**],
        'APP_DIRS':True,
        'OPTIONS': {
--my_project
 --templates
 --app_a
(after fixed)
--my_project
 --app_a
 --templates