Crampus
Crampus

Reputation: 491

django authentication view doesnt work properly

When I try to login through the login page of my django project, there is the message User with this Username already exists. Here I used my own authentication view:

def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is None:
                return HttpResponseRedirect('/logger/bad_login/')
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/')
            else:
                return HttpResponseRedirect('/logger/bad_login/')
    else:
        form = LoginForm()
    return render(request, 'logger/login.html', {'form': form})

Later I found django stock auth views and forms. But still want to know why my view doesnt work properly.

urls.py of my logger app, which used also for saving data of users activity

from django.conf.urls import patterns, include, url

from logger import views


urlpatterns = patterns('',
    url(r'^$', views.logger_view, name='log'),
    url(r'^login/$', views.login_view, name = 'login'),
    url(r'^bad_login/$', views.bad_login_view, name = 'bad_login'),
)

And template

{% extends "base.html" %}

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

{% block content %}
<form action="/logger/login/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock content %}

LoginForm

class LoginForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username', 'password')

Upvotes: 0

Views: 118

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174662

You are using a ModelForm and those will only valid if both the form and the model validation pass.

As you are using the User model, your form does not validate at the model level which is why your view fails.

If you change your login form thus, your view will work as intended:

class LoginForm(forms.Form):  # A normal form, not a model form
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

You might also want to fix your view, so your form errors are displayed correctly, and you are utilizing the form correctly as well:

def login_view(request):
    form = LoginForm(request.POST or None)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user = authenticate(username=username, password=password)
        if user is None:
            return HttpResponseRedirect('/logger/bad_login/')
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponseRedirect('/logger/bad_login/')
    return render(request, 'logger/login.html', {'form': form})

Upvotes: 1

Related Questions