Django:TemplateDoesNotExist at /login/(Source Does Not Exist)

blmhpbnm  于 2023-07-01  发布在  Go
关注(0)|答案(3)|浏览(160)

我正试图进入Django,我在这里和那里遵循教程。我试图从官方Django教程中添加一个登录页面到投票应用程序,但即使我按照this simple tutorial的信,我得到了TemplateDoesNotExist error。据我所知,Django正在正确的目录中查找login.html,并给出消息Source doesn 't exist。

我的文件结构:

├── 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试图加载正确的模板,看到正确的文件夹,但显示错误“源不存在”,即使模板在那里。
你觉得我哪里做错了吗?

rta7y2nd

rta7y2nd1#

'DIRS': [os.path.join(BASE_DIR, 'templates')],

这告诉Django在项目目录中查找templates目录(包含manage.py的目录)。但是,模板目录位于内部项目目录(包含settings.py的目录)中。
您可以通过执行以下操作之一来修复此问题:
1.移动模板目录
1.更改DIRS设置以指向模板目录的当前位置。

'DIRS': [os.path.join(BASE_DIR, 'secvot', 'templates')],

1.将secvot添加到INSTALLED_APPS中,以便app_directories加载程序找到templates目录。如果这样做,您可以将DIRS更改为[]

unftdfkk

unftdfkk2#

这是一个拼写错误!
在我的例子中,错误是
异常类型:模板不存在
异常值:blog/blog_form.html
奇怪的是,我甚至没有创建这个blog_form.html文件。
那个愚蠢的错误是什么
我在www.example.com文件中提到template_name为template_name(参见拼写)views.py。
我花了30多分钟才发现这个错误,因为大多数人都有快速阅读的习惯(而不是一个字母一个字母地读)。我也找不到那个blog_form.html文件(它甚至不存在,大部分时间都浪费在这个文件上)。所以我建议你回去找那个拼写错误。它对我有效,我希望它对你也有效,因为我知道这有多令人沮丧。

vbopmzt1

vbopmzt13#

看看这个链接,它对我同样有效,但对我来说,我还必须在我的设置DIR中指定应用程序名称和模板:https://stackoverflow.com/a/52225716/20213298

相关问题