Django:templatedoesnotextist位于/login/(源不存在)

2024-10-04 07:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试进入Django,我在这里和那里跟随教程。我试图从Django官方教程中向polling应用程序添加一个登录页面,但是即使我按照this simple tutorial的字母进行操作,我还是得到了templatedoesnotex错误。但据我所知,Django正在寻找登录.html在正确的目录中,并给出消息源不存在。在

我的文件结构:

├── 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

在设置.py公司名称:

^{pr2}$

塞沃特/网址.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),
]

在登录.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尝试加载正确的模板,查找正确的文件夹,但显示错误“Source does not exist”事件,尽管模板在那里。在

你知道我做错了什么吗?在


Tags: djangopyhomeadminlibhtmlsitelogin
1条回答
网友
1楼 · 发布于 2024-10-04 07:28:17
'DIRS': [os.path.join(BASE_DIR, 'templates')],

这告诉Django在项目目录中查找templates目录(包含manage.py的目录)。但是模板目录在内部项目目录中(包含settings.py的目录)。在

您可以通过执行以下操作之一来修复它:

  1. 移动模板目录
  2. 更改DIRS设置以指向模板目录的当前位置。在

    'DIRS': [os.path.join(BASE_DIR, 'secvot', 'templates')],
    
  3. secvot添加到INSTALLED_APPS中,以便app_directories加载程序找到模板目录。如果您这样做,您可以将DIRS更改为[]。在

相关问题 更多 >