Django测试项目TemplateDoesNotExist at / [英] Django test project TemplateDoesNotExist at /
问题描述
settings.py
$ b $
$ b使用Django 1.8.3生成的'django-admin startproject'
$ _ code $
$ b有关此文件的更多信息,请参阅
https://docs.djangoproject.com/en/1.8/topics/settings/
有关设置的完整列表和他们的价值观,参见
https://docs.djangoproject.com/en/1.8/ref/settings/
#在项目中构建这样的路径:os.path.join(BASE_DIR,...)
import os
#outside src文件夹
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath (__file__)))
#快速启动开发设置 - 不适合生产
#请参阅https://docs.djangoproject.com/en/1.8/howto/deployment / checklist /
#安全警告:保留生产密钥中使用的密钥!
SECRET_KEY ='zzyo5 * = sv3of& 2la @ $ v = 6)9x& + sn_b4sr94 == mi1 $ b *& qhej +!'
#安全警告:不要运行调试开始生产!
DEBUG = True
ALLOWED_HOSTS = []
#应用程序定义
INSTALLED_APPS =(
' django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django。
'django.contrib.staticfiles',
'注册'
)
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 = my_notebook.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 ='my_notebook.wsgi.application'
#数据库
#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'),
}
}
#国际化
#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
#静态文件(CSS,JavaScript,图像)
#https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL ='/ static /'
#templates位置
TEMPLATE_DIRS =(
os.path.join(os.path.dirname(BASE_DIR),static_1,templates),
)
出现在错误页面中。
我有一个文件在此文件夹中定义signup.html
我的视图显示为 -
从django.shortcuts import render,render_to_response,RequestContext
#在这里创建你的意见。
def home(request):
return render_to_response(signup.html,locals(),context_instance = RequestContext(request))
此视图出现在名为注册的应用程序中,该应用程序已添加到我的项目中已经存在的settings.py中 -
INSTALLED_APPS =(
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'注册'
)
最后我的项目的urls.py -
urlpatterns = patterns('',
url(r'^ $',signups.views.home,name ='home'),
url(r' admin /',include(admin.site.urls)),
)
有人可以请帮助我为什么我得到这个错误
TemplateDoesNotExist at / si gnup.html
我使用的是django 1.8.3。
谢谢提前
TEMPLATE_DIRS
设置只能配置为加载不属于任何应用程序的模板。
在您的情况下,您有一个模板 signup.html
对于注册
申请;所以你应该这样做:
-
首先,摆脱
TEMPLATE_DIRS
设置。在django 1.8中,此设置已被删除(请参阅此页面新模板配置)。 -
在同一个目录中创建一个名为
templates
的目录, c $ c> views.py 为您的注册应用程序,应该如下所示:注册/
migrations /
__init__.py
admin.py
models.py
tests.py
views.py
templates /
注册/
-
在这个
注册/模板/注册/
目录,创建您的signup.html
文件。 -
您需要的视图:
from django.shortcuts import render
def home(request):
return render(request,'signups / signup.html')
I know this question has been asked many times but even after resolving all the things I am still getting this error. My setup is as follow -
settings.py
"""
Django settings for my_notebook project.
Generated by 'django-admin startproject' using Django 1.8.3.
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
#outside src folder
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 = 'zzyo5*=sv3of&2la@$v=6)9x&+sn_b4sr94==mi1$b*&qhej+!'
# 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',
'signups'
)
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 = 'my_notebook.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 = 'my_notebook.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/'
#templates location
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static_1", "templates"),
)
as its appearing in the error page. I have a file defined in this folder signup.html
My view appears as -
from django.shortcuts import render, render_to_response, RequestContext
# Create your views here.
def home(request):
return render_to_response("signup.html", locals(), context_instance=RequestContext(request))
this view appears inside an app named signups which has been added to settings.py already in my project as -
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'signups'
)
Lastly urls.py of my project -
urlpatterns = patterns('',
url(r'^$', "signups.views.home", name='home'),
url(r'^admin/', include(admin.site.urls)),
)
Can someone please help me why i am getting this error
TemplateDoesNotExist at / signup.html
I am using django 1.8.3.
Thanks in advance
The TEMPLATE_DIRS
setting should only be configured if you are loading templates that do not belong to any application.
In your case you have a template signup.html
that is for the signups
application; so you should do this:
First, get rid of the
TEMPLATE_DIRS
setting. In django 1.8 this setting was removed anyway (see this page on the new template configuration).Create a directory called
templates
in the same directory that has theviews.py
for your signups app, it should look like this:signups/ migrations/ __init__.py admin.py models.py tests.py views.py templates/ signups/
In this
signups/templates/signups/
directory, create yoursignup.html
file.Now, in your view you need:
from django.shortcuts import render def home(request): return render(request, 'signups/signup.html')
这篇关于Django测试项目TemplateDoesNotExist at /的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!