开发者

Reverse error in Django . How to debug this?

My views.py is this

from django.contrib.auth import authenticate, login
from django.shortcuts import render_to_response
def login(request):
    def errorHandle(error):
        form = LoginForm()
        return render_to_response('login.html', {
                'error' : error,
                'form' : form,
        })
    if request.method == 'POST': # If the form has been submitted...
        form = LoginForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    # Redirect to a success page.
                    login(request, user)
                    return render_to_response('courses/logged_in.html', {
                        'username': username,
                    })
                else:
                    # Return a 'disabled account' error message
                    error = u'account disabled'
                    return e开发者_如何转开发rrorHandle(error)
            else:
                 # Return an 'invalid login' error message.
                error = u'invalid login'
                return errorHandle(error)
        else:
            error = u'form is invalid'
            return errorHandle(error)
    else:
        form = LoginForm() # An unbound form
        return render_to_response('login.html', {
            'form': form,
        })

The urls.py is this

 url(r'^userprof/login/$', 'django.contrib.auth.views.login', {'template_name': 'userprof/login.html'}),

The template login.html is this

{% block content %}

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

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
    <td>{{ form.username.label_tag }}</td>
    <td>{{ form.username }}</td>
</tr>
<tr>
    <td>{{ form.password.label_tag }}</td>
    <td>{{ form.password }}</td>
</tr>
</table>

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

{% endblock %}

The error I get is this

Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found.

What is the error . And how to remove it ?


Look in the documentation, there is a function called login_required decorator. I think this should be the easiest way for you.


Question updated a lot. See new answer in the comments for this answer.

Templates should not nor can deal with authenticating users. You need to submit the form to a view. django.auth.contrib comes with a view for this out of the box. Check out: https://docs.djangoproject.com/en/dev/topics/auth/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜