Reputation: 3378
I have a login and authentication function that takes input from the form, validate data from database and if correct returns right.html if not returns wrong.html. The password is stored in galaxy_user database, The output of uname is:
u'sachitad'
And the output of passw is:
u'f8566297ee28e8a3096497070b37b91d24c24243'
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
u = ''.join(username)
p = hashlib.sha1(password).hexdigest()
a = GalaxyUser.objects.values_list('username', 'password')
uname = a[0][0]
passw = a[0][1]
user = authenticate(uname=u, passw=p)
if user is not None:
return render_to_response('right.html', context_instance=RequestContext(request))
else:
return render_to_response('wrong.html', context_instance=RequestContext(request))
else:
return render_to_response('login.html', context_instance=RequestContext(request))
EDIT: GalaxyUser table:
id | create_time | update_time | email | password | external | deleted | purged | username | form_values_id | disk_usage |
+----+---------------------+---------------------+-------------------------------+------------------------------------------+----------+---------+--------+----------+----------------+------------+
| 1 | 2013-01-11 15:00:30 | 2013-01-11 15:00:30 | [email protected] | f8566297ee28e8a3096497070b37b91d24c24243 | 0 | 0 | 0 | sachitad | NULL | NULL |
| 2 | 2013-01-11 15:01:01 | 2013-01-11 15:01:01 | [email protected] | f8566297ee28e8a3096497070b37b91d24c24243 | 0 | 0 | 0 | saugat | NULL | NULL |
+----+---------------------+---------------------+-------------------------------+------------------------------------------+----------+---------+--------+-
I want to authenticate with email and password.
Upvotes: 2
Views: 2918
Reputation: 6238
authenticate() takes two keyword arguments, username
and password
.
user = authenticate(username=u, password=p)
EDIT:
It's hard to tell what the problem is, since we don't have any debugging information...you might be registering the user incorrectly in your Registration function, therefore setting the username or password incorrectly. This would explain why you're being redirected to the wrong.html page. The following Login function works for me:
from django.shortcuts import render_to_response, redirect
from django.http import HttpResponseRedirect
from django.template import RequestContext
from somewhere.forms import LoginForm
from django.contrib.auth import authenticate, login
def Login(request):
if request.method == 'POST':
login_form = LoginForm(request.POST) # Bound LoginForm
if login_form.is_valid():
username = login_form.cleaned_data['username']
password = login_form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/success/')
else:
# user has not been activated...must activate using the following lines:
#### request.session['username'] = username
#### user.backend = 'django.contrib.auth.backends.ModelBackend'
#### user.is_active = True
#### user.save()
else:
# u_and_p_error = "Username and password did not match"
else:
return render_to_response('index.html', {'login_form': login_form}, context_instance=RequestContext(request))
Upvotes: 0
Reputation: 772
Django stores the password as hexdigest of the given plaintext password and salt using the given algorithm ('md5', 'sha1' or 'crypt')
authenticate() takes the password you provided at the time of registering the user and not the hashed version of it. You need to provide authenticate the plain text password to authenticate the user.
Upvotes: 2