我试图进入Django并且我在这里和那里按照教程进行操作。我试图从官方的Django教程中添加一个登录页面到轮询应用程序,但即使我在this simple tutorial跟随这封信,我收到了TemplateDoesNotExist错误。据我所知,Django正在寻找正确目录中的login.html并给出消息Source不存在。
我的文件结构:
├── db.sqlite3
├── manage.py
├── secvot
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── templates
│ │ ├── admin
│ │ │ └── base_site.html
│ │ └── registration
│ │ ├── login.html
│ │ └── logout.html
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
└── voting
├── admin.py
├── admin.pyc
├── apps.py
├── apps.pyc
├── __init__.py
├── __init__.pyc
├── migrations
│ ├── 0001_initial.py
│ ├── 0001_initial.pyc
│ ├── 0002_auto_20180301_2354.py
│ ├── 0002_auto_20180301_2354.pyc
│ ├── __init__.py
│ └── __init__.pyc
├── models.py
├── models.pyc
├── static
│ └── voting
│ ├── images
│ │ └── background.gif
│ └── style.css
├── templates
│ └── voting
│ ├── detail.html
│ ├── index.html
│ └── results.html
├── tests.py
├── tests.pyc
├── urls.py
├── urls.pyc
├── views.py
└── views.pyc
settings.py:
INSTALLED_APPS = [
'voting.apps.VotingConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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 = 'secvot.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',
],
},
},
secvot / urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^login/', auth_views.login, {'template_name': 'registration/login.html'}, name='login.html'),
url(r'^logout/', auth_views.logout, name='logout'),
url(r'^polls/', include('voting.urls')),
url(r'^admin/', admin.site.urls),
]
的login.html:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST">
{% csrf_token %}
<p>
<label>Username</label>
<input type="text" name="username">
</p>
<p>
<label>Password</label>
<input type="password" name="password">
</p>
<button type="submit">Login</button>
</form>
</body>
</html>
和错误页面的一部分:
TemplateDoesNotExist at /login/
registration/login.html
Request Method: GET
Request URL: http://127.0.0.1:8000/login/
Django Version: 1.11
Exception Type: TemplateDoesNotExist
Exception Value:
registration/login.html
Exception Location: /home/kostis/.virtualenvs/secretvoting/local/lib/python2.7/site-packages/django/template/loader.py in select_template, line 53
Python Executable: /home/kostis/.virtualenvs/secretvoting/bin/python
Python Version: 2.7.12
Python Path:
['/home/kostis/PycharmProjects/secvot',
'/home/kostis/.virtualenvs/secretvoting/lib/python2.7',
'/home/kostis/.virtualenvs/secretvoting/lib/python2.7/plat-x86_64-linux-gnu',
'/home/kostis/.virtualenvs/secretvoting/lib/python2.7/lib-tk',
'/home/kostis/.virtualenvs/secretvoting/lib/python2.7/lib-old',
'/home/kostis/.virtualenvs/secretvoting/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/kostis/.virtualenvs/secretvoting/local/lib/python2.7/site-packages',
'/home/kostis/.virtualenvs/secretvoting/lib/python2.7/site-packages']
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /home/kostis/PycharmProjects/secvot/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/kostis/PycharmProjects/secvot/voting/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/kostis/.virtualenvs/secretvoting/local/lib/python2.7/site-packages/django/contrib/admin/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/kostis/.virtualenvs/secretvoting/local/lib/python2.7/site-packages/django/contrib/auth/templates/registration/login.html (Source does not exist)
据我所知,Django尝试加载正确的模板,查看正确的文件夹,但显示错误&#34; Source不存在&#34;事件,虽然模板在那里。
关于我可能做错的任何想法?
答案 0 :(得分:2)
'DIRS': [os.path.join(BASE_DIR, 'templates')],
这告诉Django在项目目录中查找templates
目录(包含manage.py
的目录。但是你的模板目录在你的内部项目目录中(包含settings.py
的目录)。
您可以通过执行以下操作之一来解决此问题:
将DIRS
设置更改为指向模板目录的当前位置。
'DIRS': [os.path.join(BASE_DIR, 'secvot', 'templates')],
secvot
添加到INSTALLED_APPS
,以便app_directories
加载程序找到模板目录。如果您这样做,可以将DIRS
更改为[]
。答案 1 :(得分:0)
在我的情况下,错误是
异常类型:TemplateDoesNotExist
异常值:blog / blog_form.html
奇怪的是,我什至没有创建这个blog_form.html文件。
那是什么愚蠢的错误?
我在views.py文件中提到了template_name为tempplate_name(参见拼写)。
我花了30多分钟才发现这个错误,因为大多数人都有快速阅读的习惯(而不是逐字阅读)。我也找不到那个blog_form.html文件(甚至不存在,并且大部分时间都因为这个文件而浪费了)。因此,我建议您返回并发现该拼写错误。它对我有用,我希望它也对您有用,因为我知道它令人沮丧。