[英]django error TemplateDoesNotExist
I am new to Django and just follow the Django official document, but here is a problem.我是 Django 新手,只是按照 Django 官方文档进行操作,但这里有一个问题。
I create a new Django project with我创建了一个新的 Django 项目
Django 1.8.2 + PyCharm 4.5.1 + Python 3.4.3 + Windows 8.1 Django 1.8.2 + PyCharm 4.5.1 + Python 3.4.3 + Windows 8.1
- mysite
- main
- migrations
__init__.py
__init__.py
admin.py
models.py
tests.py
views.py
- mysite
__init__.py
settings.py
urls.py
wsgi.py
- templates
hello.html
db.sqlite3
manage.py
most of these are created automatically, which i modified are as follows:其中大部分是自动创建的,我修改如下:
templates/hello.html模板/你好.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Django test</title>
</head>
<body>
hello world!
</body>
</html>
mysite/urls.py我的网站/网址.py
from django.conf.urls import include, url
from django.contrib import admin
from main.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/$', hello),
]
main/views.py主/视图.py
from django.shortcuts import render_to_response
def hello(request):
return render_to_response('hello.html', locals())
and click run and visit localhost:8080, here are results:然后点击运行并访问localhost:8080,结果如下:
TemplateDoesNotExist at /hello/
hello.html
Request Method: GET
Request URL: http://127.0.0.1:8000/hello/
Django Version: 1.8.2
Exception Type: TemplateDoesNotExist
Exception Value:
hello.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:\\Users\\kant\\Desktop\\code\\PycharmProjects\\mysite',
'C:\\Users\\kant\\Desktop\\code\\PycharmProjects\\mysite',
'C:\\Windows\\SYSTEM32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34\\lib',
'C:\\Python34',
'C:\\Python34\\lib\\site-packages']
if I change the views.py as如果我将 views.py 更改为
from django.http import HttpResponse
def hello(request):
return HttpResponse("hello world")
it runs correctly.它运行正确。
I think it may caused by the template path, here is the settings.py, generated automatically without modification:我觉得可能是模板路径造成的,这里是settings.py,不加修改自动生成:
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.8.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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 = 'p(fs&6e2kg6d3%0txc+9o=(!*8fzt8w5l6neuqer*m9qictsl$'
# 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',
'main',
)
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 = 'mysite.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 = 'mysite.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 = (
os.path.join(BASE_DIR, 'templates'),
)
Your problem is with your settings.你的问题是你的设置。 You currently have:
您目前拥有:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
This is how you set up template directories in Django 1.7.x and below.这是在 Django 1.7.x 及更低版本中设置模板目录的方式。 In Django 1.8.x, change your TEMPLATES [] to read like this:
在 Django 1.8.x 中,将您的 TEMPLATES [] 更改为如下所示:
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',
],
},
},
]
Thank you for being so thorough with your question.感谢您对您的问题如此透彻。 Good luck!
祝你好运!
Try putting the template inside a subdirectory:尝试将模板放在子目录中:
<project>/<app>/templates/<app>/hello.html
and和
return render_to_response('<app>/hello.html', locals())
If you want to access templates inside <project>/templates
then you have to specify DIRS
inside TEMPLATES
如果要访问
<project>/templates
则必须在TEMPLATES
指定DIRS
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',
],
},
},
]
Probably you would have forgot to include the name of the app in the settings.py under the INSTALLED_APPS.您可能忘记在 INSTALLED_APPS 下的 settings.py 中包含应用程序的名称。 This might help someone I believe.
这可能会帮助我相信的人。
settings.py设置.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, ''),
],
views.py视图.py
return render(request, "", {})返回渲染(请求,“”,{})
example :例子 :
return render(request, "pages/template/home.html", {})返回渲染(请求,“页面/模板/home.html”,{})
Much easier solution inside your template folder make another folder and name it your app name so your file tree should look something like this:模板文件夹中更简单的解决方案创建另一个文件夹并将其命名为您的应用程序名称,因此您的文件树应如下所示:
- mysite
- main
- migrations
__init__.py
__init__.py
admin.py
models.py
tests.py
views.py
- mysite
__init__.py
settings.py
urls.py
wsgi.py
- templates
-mysite
hello.html
db.sqlite3
manage.py
I had the same error but didn't know what is causing it.. I realized I had not added my app to the list of Django installed apps list.我有同样的错误,但不知道是什么导致了它。我意识到我没有将我的应用程序添加到 Django 已安装应用程序列表的列表中。 Prevously it was as:
以前是这样的:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
And now I have added my app then it looks like现在我添加了我的应用程序然后它看起来像
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
]
Try it this way , make a change in your settings.py试试这种方式,在你的settings.py中进行更改
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join( BASE_DIR ,'projectname/templates/')
],`enter code here`
'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',
],
},
},
] ]
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from main.views import hello # this is the error
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/$', hello), # bad bad bad
]
you have to import urls.py of your app main and you are import views你必须导入你的 app main 的 urls.py 并且你正在导入视图
change your code like this像这样改变你的代码
urls.py #in folder mysite/ urls.py #in 文件夹 mysite/
from django.conf.urls import path, include, url
from django.contrib import admin
from . import views ##add this line
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('main/', include(main.urls), name='main'), ## add this line
]
you have to create a new file in your app main named urls.py #in folder main/您必须在您的应用程序主目录中创建一个名为 urls.py #in folder main/ 的新文件
from django.urls.conf import path
from . import views
app_name = 'main'
urlpatterns = [
path('', views.hello, name="hello"),
]
the last file you created in the main folder say to your project to have urls on main app您在主文件夹中创建的最后一个文件告诉您的项目在主应用程序上有网址
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.