Python Django教程中的TemplateDoesNotExist at/polls/-in
我一直在试用Django教程(文档),现在被这个错误困扰了两天。我将在下面粘贴我的views.py、settings.py和我的目录结构。 视图.PyPython Django教程中的TemplateDoesNotExist at/polls/-in,python,django,django-models,django-templates,django-urls,Python,Django,Django Models,Django Templates,Django Urls,我一直在试用Django教程(文档),现在被这个错误困扰了两天。我将在下面粘贴我的views.py、settings.py和我的目录结构。 视图.Py from django.shortcuts import render from django.http import HttpResponse from django.template import RequestContext, loader from polls.models import Poll # Create your view
from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext, loader
from polls.models import Poll
# Create your views here.
#def index(request):
#return HttpResponse("Hello, world. You are at the poll index.")
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = RequestContext(request, {
'latest_poll_list': latest_poll_list,
})
return HttpResponse(template.render(context))
def detail(request,poll_id):
return HttpResponse("You're looking at the results of the poll %s." % poll_id)
def results(request, poll_id):
return HttpResponse("You're looking at the results of poll %s." % poll_id)
def vote(request,poll_id):
return HttpResponse("You're voting on poll %s." % poll_id)
设置.Py
"""
Django settings for mysite project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ma_x5+pnvp$o7#5g#lb)0g$sa5ln%k(z#wcahwib4dngbbe9^='
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_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',
'polls',
)
MIDDLEWARE_CLASSES = (
'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 = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'C://Python34/mysite/db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
Py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
我认为问题在于指向/mysite/polls/templates/polls/index.html中的index.html文件。
这是我的文件结构mysite/polls/templates/polls/index.html
如果有人能解决这个问题,那将是一个很大的帮助。任何帮助都将不胜感激。提前谢谢 您应该将模板目录添加到
设置.py
文件中,如下所示:
TEMPLATE_DIRS=(
"C:/python27/Lib/site-packages/django/bin/project1/templates",
)
请尝试用我在下面编写的代码块替换模板,复制并替换模板:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['polls/templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
我认为您的模板_dirs=[]变量工作不正常。如果您的帐户在1.9上,那么它会贬值,并且被过度写入/忽略。如果你的templates文件夹位于不同的位置,那么只需在那里写一些疯狂的东西,并在回溯中查找它,然后将其与你想要的路径进行比较,然后从那里开始
希望这有助于在将模板目录添加到
setting.py时使用绝对路径,方法如下:
'DIRS': ["C:\\Users\username\project\\templates"],
您只需在模板文件夹中创建polls文件夹,如中所示
现在我们可以直接放置模板了
在轮询/模板中(而不是创建另一个轮询子目录),
但这实际上是个坏主意。Django将选择第一个
模板,它会查找与之匹配的名称,以及是否有与匹配的模板
在不同的应用程序中使用相同的名称,Django将无法
区分它们。我们需要把Django指向
正确的一个,确保这一点的最好方法是给它们命名。
也就是说,将这些模板放在另一个名为的目录中
应用程序本身
就像
问候。你为什么评论TEMPLATE\u DIRS
?django将在这里找到您的模板。是您发布项目的主URL.py的URL.py,还是来自polls应用程序的URL.py?教程的相关部分:@andrey:对不起,我在尝试其他东西,所以评论了template_dirs。即使在我使用模板_dirs.@shtuff.It:its polls url.py时,它也给出了相同的错误/mysite/polls/url.py它在回溯中查看的最后一个位置是:C:\Python34\mysite\polls\templates\polls\index.html,它似乎与您指示模板所在的位置相匹配,但它表示文件不存在。此路径是否与您放置index.html的位置匹配?
'DIRS': ["C:\\Users\username\project\\templates"],