使用@login_required装饰器

时间:2014-02-19 14:04:17

标签: django django-templates django-views django-authentication django-login

这是我第一次使用用户身份验证编写自定义视图。我被困了几个小时,因为我真的不知道我应该注意什么或者我应该注意什么,使用 @login_required 装饰器。这是我的 views.py

from django.shortcuts import render
from programm.models import *
from django.contrib.auth.decorators import login_required

def index(request):
        return render(request, 'index.html')

@login_required
def liste(request):
        return render(request, 'liste.html', {'lObj': learningObjective.objects.all()})

这些是我的应用 urls.py

from django.conf.urls import patterns, url
from programm import views
from django.conf import settings

urlpatterns = patterns('',
        url(r'^$', views.index, name = 'index'),
        url(r'^liste.html$', views.liste, name = 'liste'),
)

这是 login.html 模板:

{% block body %}
 <header>
  <h1>Login Page</h1>
 </header>

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

    <form method="post" action=".">
      {% csrf_token %}
      <p>
        <label for="id_username">Username:</label>
        {{ form.username }}
      </p>
      <p>
        <label for="id_password">Password:</label>
        {{ form.password }}
      </p>
      {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
      {% else %}
        <input type="hidden" name="next" value="/programm/liste.html" />
      {% endif %}
      <input type="submit" value="login" />
    </form>   </section> {% endblock %}
  </section>
{% endblock %}

你能告诉我我必须注意什么以及我必须添加什么吗?我找不到我理解正确的指南:/我现在得到的错误信息如下:

TemplateDoesNotExist at /login/

registration/login.html

Request Method:     GET
Request URL:    http://10.0.3.107:8000/login/?next=/programm/liste.html
Django Version:     1.6.1
Exception Type:     TemplateDoesNotExist
Exception Value:    

registration/login.html

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/template/loader.py in find_template, line 131
Python Executable:  /usr/bin/python
Python Version:     2.7.3
Python Path:    

['/home/ubuntu/NachweisProj/ANachweis',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages']

Server time:    Wed, 19 Feb 2014 14:08:12 +0000

1 个答案:

答案 0 :(得分:0)

您尚未向我们展示模板的布局 - 登录视图要求“login.html”模板位于“注册”文件夹中。