I am making a login form in django. When I am running the app and filled the username and password fields. The page always redirected to one condition (whether the username is right or not).
The code is as :
def home(request):
if request.method == 'POST':
username = request.POST.get('user_name')
password = request.POST.get('password')
user = authenticate(user_name=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# success
return render_to_response('registration/main_page.html',{'form':login},context_instance=RequestContext(request))
else:
#user was not active
return render_to_response('registration/q.html')
else:
# not a valid user
return render_to_response('registration/home.html')
else:
# URL was accessed directly
return render_to_response('registration/w.html')
It always redirected to home.html
else:
#user was not active
return render_to_response('registration/home.html')
Why it happens?
authenticate()function takesusernamenotuser_name.Try this:
user = authenticate(username=username, password=password)Also, instead of
if user is not None:you can writeif user :