bfissa
bfissa

Reputation: 429

Django Form request.POST.get() always returns empty

Sorry if this is a noob question. I'm creating a login form for a Django app, but I'm having trouble getting it to work. request.POST.get() doesn't return anything, so the authentication always fails. Am I missing something obvious?

login.html:

{% extends "base.html" %}

{% block content%}

<h2>Welcome! Please login to continue.</h2> <br>
<form action="{% url 'account:authenticate' %}" method="post">
    {% csrf_token %}
    <div >
        <label for='username'> Username: </label>
        <input type='text' name='Username' id='username'> <br><br>
        <label for='password'>Password:  </label>
        <input type='password' name='Password' id='password'><br><br>
        <input type='submit' value='Login'>
    </div>
</form>

relevant part of views.py:

from django.shortcuts import render, get_object_or_404
from datetime import datetime   
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import loader
from random import randrange
from django.contrib.auth import authenticate

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

def authenticate(request):
    usern = request.POST.get('username', '')
    passw = request.POST.get('password', '')
    user = authenticate(username = usern, password = passw)      
    if user is not None:
        authenticate.login(request, user)
        return HttpResponseRedirect('/voters/instructions')
    else:
        return HttpResponseRedirect('/account/loginfail')

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

I'm using Django 1.10. Thanks!

Upvotes: 9

Views: 11683

Answers (1)

Girish Patil
Girish Patil

Reputation: 154

Check out the names in the form input fields they are case sensitive. in Django do this

 usern = request.POST.get('Username', '')
 passw = request.POST.get('Password', '')

or in html form make them lowercase the input name field

Upvotes: 8

Related Questions