TemplateDoesNotExist 处的错误 /
Posted
技术标签:
【中文标题】TemplateDoesNotExist 处的错误 /【英文标题】:Error at TemplateDoesNotExist at / 【发布时间】:2017-03-03 17:16:56 【问题描述】:我正在使用 django 1.10.2 版本,我收到一个错误,显示 TemplateDoesNotExist at /
我不知道为什么会这样,我尝试修复它:
“目录”: ['C:\Users\rahul\django-tuts\cms\blog\templates\blog\post'],
或
'DIRS': [os.path.join(os.path.dirname(file), '..//', '模板').replace('\', '/')],
仍然遇到同样的错误。
以下是我的文件内容:
settings.py
Django settings for cms project.
Generated by 'django-admin startproject' using Django 1.10.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'u2!61zgb-klaf=qh)bh@9brkxt%vy5!!esg5=uvs!ahh%@t$-4'
# 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',
'blog',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'cms.urls'
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
# 'DIRS': ['C:\\Users\\rahul\\django-tuts\\cms\\blog\\templates\\blog\\post'],
# 'DIRS': [os.path.join(os.path.dirname(__file__), '..//', 'templates').replace('\\', '/')],
'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 = 'cms.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES =
'default':
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
,
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
,
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
,
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
,
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/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.10/howto/static-files/
STATIC_URL = '/static/'
urls.py
"""cms URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('blog.urls', namespace='blog', app_name='blog')),
]
views.py
from django.shortcuts import render
from .models import Post
# Create your views here.
def list_of_post(request):
post = Post.objects.all()
template = 'blog/post/list_of_post.html'
context = 'post': post
return render(request, template, context)
【问题讨论】:
请展示您的项目布局,包括blog/post/list_of_post.html
模板的位置。
顺便说一句,您应该从包含您的博客网址的正则表达式中删除美元:url(r'^', include('blog.urls', namespace='blog', app_name='blog')),
【参考方案1】:
我认为你应该考虑你的项目根:
考虑到您的templates/blog
文件夹,应该是这样的:
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates/blog'],
'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',
],
,
,
]
然后要使用模板,您只需指定templates/blog
文件夹中的相对路径:
from django.shortcuts import render
from .models import Post
# Create your views here.
def list_of_post(request):
post = Post.objects.all()
template = 'post/list_of_post.html'
context = 'post': post
return render(request, template, context)
它应该可以工作。
【讨论】:
以上是关于TemplateDoesNotExist 处的错误 /的主要内容,如果未能解决你的问题,请参考以下文章
Pythonanywhere 中的 TemplateDoesNotExist 错误
解决django TemplateDoesNotExist错误