Django:位于/ accounts / login /的TemplateDoesNotExist

时间:2018-10-13 02:36:31

标签: django

我正在关注Mozilla.org上的tutorial

设置身份验证视图

我在这部分遇到问题:

下一步是在搜索路径上创建注册目录,然后添加login.html文件。

因为我已经创建了该目录和模板,但是我的应用程序无法识别它。

结构:

Note: Your folder structure should now look like the below:
locallibrary (Django project folder)
   |_catalog
   |_locallibrary
   |_templates (new)
                |_registration

我的结构:

enter image description here

然后针对这一部分:

使这些目录对模板加载器可见

我已经:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        #'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'DIRS': ['./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',
            ],
        },
    },
]

项目url.pý:

from django.contrib import admin
from django.urls import path, include
from main_app import views



urlpatterns = [
    path('accounts/', include('django.contrib.auth.urls')),
    path('admin/', admin.site.urls),
    path('', include('main_app.urls')),

]

对于我的login.html模板,我已经:

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">

    <title>Jumbotron Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="../../dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="jumbotron.css" rel="stylesheet">
    <!-- Custom styles for this template -->


    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" rel="stylesheet">
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" rel="stylesheet">

    <link href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" rel="stylesheet"/>


    <link href="{% static 'main_app/style.css' %}" rel="stylesheet">


</head>

{% block content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

{% if next %}
    {% if user.is_authenticated %}
    <p>Your account doesn't have access to this page. To proceed,
    please login with an account that has access.</p>
    {% else %}
    <p>Please login to see this page.</p>
    {% endif %}
{% endif %}

<form method="post" action="{% url 'login' %}">
{% csrf_token %}

<div>
  <td>{{ form.username.label_tag }}</td>
  <td>{{ form.username }}</td>
</div>
<div>
  <td>{{ form.password.label_tag }}</td>
  <td>{{ form.password }}</td>
</div>

<div>
  <input type="submit" value="login" />
  <input type="hidden" name="next" value="{{ next }}" />
</div>
</form>

{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>

{% endblock %}


</html>

根据该教程,我应该导航至:

http://127.0.0.1:8000/accounts/login/

看到这个:

enter image description here

但是我看到了:

TemplateDoesNotExist位于/​​ accounts / login / registration / login.html

enter image description here

3 个答案:

答案 0 :(得分:2)

您不得在项目设置文件夹中创建模板。

您可以在项目根目录中创建模板目录,如下所示:

gallito (Django project folder)
   |_gallito
   |_templates (new)
                |_registration

您可以看到模板目录位于应用程序目录旁边的项目根目录中。不在您的设置目录中。

您还可以将模板目录移至项目模板目录

答案 1 :(得分:1)

这应该解决它。

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

记住:

Django为身份验证视图提供no default template。您应该为要使用的视图创建own模板。模板上下文记录在每个视图中,请参阅所有身份验证视图。

阅读以下内容: https://docs.djangoproject.com/en/3.1/topics/auth/default/#module-django.contrib.auth.views

答案 2 :(得分:1)

在 settings.py 文件中的 INSTALLED_APPS 中添加您的应用程序名称 gallito 如果你的代码是这样的

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

然后改成这个

INSTALLED_APPS = [

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'gallito',
]