Question

It is my first time writing a custom view with user authentication. I am stuck since hours because I really have no clue, to what I should pay attention to or what I should be aware of, using the @login_required decorator. Here are my 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()})

These are my app 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'),
)

This is the login.html template:

{% 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 %}

Can you guys please tell me what I have to be aware of and what I have to add ? I didn't find a guide that I understood properly :/ The error message I get at the moment is following :

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
Was it helpful?

Solution

You haven't shown us the layout of your templates -- the login view expects the "login.html" template to be in a folder "registration".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top