authenticate function is always returning none in views.py

11 views
Skip to first unread message

rocky101

unread,
Feb 21, 2017, 10:01:52 AM2/21/17
to Django users
I'm trying to sign in with an account that already exists in the system, i checked the admin django page of my project to make sure it was there, but everytime i enter the username and password it always redirects me to the invalid webpage when I know the username and password is the exact same as stored in my django login database that is already provided. The login worked well with my previous html login page so I know it can work properly but with this new html login page it is not working. What may be the problem as to why my authenticate function is always returning none? Thanks


Here is my views.py file :

def login(request):
    c={}
    c.update(csrf(request))
    return render_to_response('templates/login3.html',c)
def auth_view(request):
    username=request.POST.get('username','')
    password=request.POST.get('password','')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request,user)
        return HttpResponseRedirect('/Source.html')
    else:
        return HttpResponseRedirect('/accounts/invalid/')

This is my html templates with my login form:

<html>

<link rel="stylesheet" type="text/css" href="static/bootstrap-cosmo.min.css">


<link rel="stylesheet" type="text/css" href="static/bootstrap-cosmo.css">


<form class="form-horizontal" action="/accounts/auth/" method="post">
    {% csrf_token %}

  <fieldset>
    <legend>LOGIN</legend>
    <div class="form-group">
      <label for="username" class="col-lg-2 control-label">Username</label>
      <div class="col-lg-10">
        <input type="text" class="form-control" id="username" placeholder="username">
      </div>
    </div>
    <div class="form-group">
      <label for="password" class="col-lg-2 control-label">Password</label>
      <div class="col-lg-10">
        <input type="password" class="form-control" id="password" placeholder="Password">

    </div>
    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
        <button type="reset" class="btn btn-default">Cancel</button>
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
    </div>
  </fieldset>
</form>




</html>






Melvyn Sopacua

unread,
Feb 21, 2017, 12:07:07 PM2/21/17
to django...@googlegroups.com

Your inputs don't have a "name" attribute, so I suspect your username and password are always empty.

 

On Monday 20 February 2017 22:11:56 rocky101 wrote:

 

> username=request.POST.get('username','')

> password=request.POST.get('password','')

> user = auth.authenticate(username=username, password=password)

 

> <input type="text" class="form-control" id="username"

 

> <input type="password" class="form-control" id="password"

> placeholder="Password">

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages