尝试使用Django实现登录应用程序时出错

时间:2017-06-14 09:25:36

标签: python django

我面临一个问题。我正在尝试使用Django实现login/logup应用程序,但在登录页面中我收到以下错误。

TemplateDoesNotExist at /login/
login.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/login/
Django Version: 1.11.2
Exception Type: TemplateDoesNotExist
Exception Value:    
login.html
Exception Location: /usr/local/lib/python2.7/dist-packages/django/template/loader.py in select_template, line 53
Python Executable:  /usr/bin/python
Python Version: 2.7.6

我正在解释下面的login.html页面。

{% extends 'base.html' %}

{% block title %}Login{% endblock %}

{% block content %}
  <h2>Login</h2>
  <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Login</button>
  </form>
{% endblock %}
  

urls.py:

from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.views.generic.base import TemplateView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
    url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
    url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
]

请帮我解决此错误。

2 个答案:

答案 0 :(得分:0)

在settings.py中设置模板目录路径

  

os.path.join(BASE_DIR,&#39; templates&#39;)

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.template.context_processors.static',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

您的项目结构:

project
  |---- project
  |---- templates
  |----- static

答案 1 :(得分:0)

我认为您需要在settings.py中设置模板DIRS,如下所示:

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates/ariticle'), ], # define your template dir
        ...
    },
]
相关问题