位于/base.html的TemplateDoesNotExist [英] TemplateDoesNotExist at / base.html

查看:76
本文介绍了位于/base.html的TemplateDoesNotExist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友。
我尝试通过做一本书中的项目示例来重复:Sanjeev的Jaiswal撰写的学习Django Web开发。

friends. I try to repeat by doing the example of project in book: "Learning Django Web Development" by Jaiswal, Sanjeev.

运行服务器时,我遇到了这样的异常:位于/base.html

Running the server i get such exception: 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



< p > Template-loader postmortem

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)

我的settings.py文件:

My settings.py file:

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的方法:

I tried to change settings.py in such way too:

已更改settings.py:

changed 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',
    ),
)

我的项目结构:

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)),
)

跟踪:

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

请给我个建议,我应该怎么改变才能显示页面?

Please, give an advice, what should i change to get rendered page?

推荐答案

我对您的书不熟悉正在使用,因此我无法根据此提供任何建议。如果这本书是针对Django 1.7的,那么至少在使用Django时,您会发现使用Django 1.7而不是Django 1.8更容易。

I'm not familiar with the book you are using, so I can't give you any advice based on that. If the book is for Django 1.7, you will find it easier to use Django 1.7 instead of Django 1.8, at least when you are beginning with Django.

坚持使用Django 1.8,以下是解决您当前看到的错误的方法:

If you want to stick with Django 1.8, here's how to fix the error you are currently seeing:

您的 settings.py 文件具有混合了旧模板设置,例如 TEMPLATE_DIRS TEMPLATE_LOADERS (Django< = 1.7),以及模板(Django 1.8+)。

Your settings.py file has a mixture of old templates settings, like TEMPLATE_DIRS and TEMPLATE_LOADERS (Django <= 1.7), and the new settings under TEMPLATES (Django 1.8+).

首先,删除旧设置 TEMPLATE_DIRS TEMPLATE_LOADERS

First, remove the old settings TEMPLATE_DIRS and TEMPLATE_LOADERS.

其次,在您的 TEMPLATES DIRS 是不正确的c $ c>设置。

Secondly, it looks as if DIRS is incorrect in your TEMPLATES setting.

定义 BASE_DIR ,应将其包含在设置中。 py 默认情况下,当您运行 ./ manage.py startproject

Define BASE_DIR, which should be included in settings.py by default when you run ./manage.py startproject

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

然后将模板更改为

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

这篇关于位于/base.html的TemplateDoesNotExist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆