Django user login form user is none
<p>I'm having some trouble logging in users. I have the following views and template:</p>

<p>views.py</p>

<pre><code>def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = auth.authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                auth.login(request, user)
                return HttpResponseRedirect('/')
            else:
                return HttpResponseRedirect('disabled account')

        else:
            return HttpResponseRedirect('/invalid')

    else:
        return render(request, 'login_page.html', {})
</code></pre>

<p>login_page.html</p>

<pre><code>{% block content %}

    {% if form.errors %}
        &lt;p class="error"&gt; sorry, thats not a valid username or password&lt;/p&gt;
    {% endif %}

&lt;form action="/login/" method="post"&gt; 
    &lt;label for="username"&gt;User name:&lt;/label&gt;
    &lt;input type="text" name="username" value="" id="username"&gt;
    &lt;label for="password"&gt;Password:&lt;/label&gt;
    &lt;input type="text" name="password" value="" id="password"&gt;

    &lt;input type="submit" value="login" /&gt;

&lt;/form&gt;

{% endblock %}
</code></pre>

<p>My problem is, whenever i hit submit in the login form, no matter what the values are, the page is redirected to the /invald/ url. I have a user set up using the admin function but i still keep getting this error. Does anyone know what the problem may be?</p>

<p>Do i need a forms.py file with anything in it?</p>

<p>Updated views:</p>

<pre><code>def login(request):
    form = LoginForm(request.POST)
    if form.is_valid():
         username = form.cleaned_data["username"]
         password = form.cleaned_data["password"]
         user = authenticate(username=username, password=password)

         if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('loggedin.html')
            else:
                return HttpResponse("Account deleted or disabled")


    return render(request, "login_page.html", {'form': form})
</code></pre>

<p>updated form:</p>

<pre><code>from django import forms
from django.contrib.auth import authenticate, login, logout, get_user_model

class LoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

def clean(self, *args, **kwargs):
    username = self.cleaned_data.get("username")
    password = self.cleaned_data.get("password")
    if username and password:
        user = authenticate(username=username, password=password)
        if not user:
            raise forms.ValidationError("User does not exist.")
        if not user.is_active:
            raise forms.ValidationError("User is no longer active.")
    return super(UserLoginForm, self).clean(*args, **kwargs)
</code></pre>

<p>updated login_page.html</p>

<pre><code>{% block content %}

    {% if form.errors %}
        &lt;p class="error"&gt; sorry, thats not a valid username or password&lt;/p&gt;
    {% endif %}

&lt;form action="{% url 'login' %}" method="post"&gt; 
    {{  form  }}
    &lt;input type="submit" value="login" /&gt;
&lt;/form&gt;

{% endblock %}
</code></pre>

<p>urls:</p>

<pre><code>    url(r'^admin/', admin.site.urls),
    url(r'^login/', views.login, name='login'),
    url(r'^logout/', views.logout, name='logout'),
    url(r'^invalid/', views.invalid_login, name='invalid_login'),
    url(r'^loggedin/', views.loggedin, name='loggedin'),
    url(r'^$', views.index, name='index'),
</code></pre>

<p>I suggest you make a forms.py file and in here make a class like so:</p>

<pre><code>from django import forms
from django.contrib.auth import authenticate, login, logout, get_user_model

    class LoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

    def clean(self, *args, **kwargs):
        username = self.cleaned_data.get("username")
        password = self.cleaned_data.get("password")
        if username and password:
            user = authenticate(username=username, password=password)
            if not user:
                raise forms.ValidationError("User does not exist.")
            if not user.is_active:
                raise forms.ValidationError("User is no longer active.")
        return super(UserLoginForm, self).clean(*args, **kwargs)
</code></pre>

<p>Then in your views import Login form. Create a view as such:</p>

<pre><code>def my_login_view(request):
    form = LoginForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data["username"]
        password = form.cleaned_data["password"]
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('someurl')
            else:
                return redirect('someotherurl')
    return render(request, "your_template_to_process_this_view", {'form': form})
</code></pre>

<p>In your html template you can call the form using the convention below. For this shortcut to the url to work you need an app_name = "yourappname" at the top of your url file.</p>

<pre><code>&lt;form method="POST" action="{% url 'yourappname:name_given_to_url_in_urls_file' %}"&gt;
  {% csrf_token %}
  {{ form }}
&lt;input type="submit" class="btn-success" value="Login"/&gt;
&lt;/form&gt;
</code></pre>

python html django forms login