0
votes

I'm newbie to Django. I'm stuck at one thing that, my login authenticate method return None. I've tried my best to search and try to solve this but i didn't find my problem solution. In Following First Code Sample From Which I Got Error,

`def loginAdmin(request):

if request.method == "POST":
    username = request.POST['username'],
    password = request.POST['password'],
    user = auth.authenticate(username = username, password = password)
    if user is not None:
      auth.login(request, user)  
      return redirect('/admin-dashboard')
    else:
        messages.info(request, 'invalid')
else:
    return HttpResponse('No')
    #  return render(request,'login.html') `

I'm getting value of form and using variable in authenticate method but its giving me that error. But in Following Second Code Sample That Execute Successfully ` def loginAdmin(request):

if request.method == "POST":
    username = request.POST['username'],
    password = request.POST['password'],
    user = auth.authenticate(username = 'user1', password = 'pass123')
    if user is not None:
      auth.login(request, user)  
      return redirect('/admin-dashboard')
    else:
        messages.info(request, 'invalid')
else:
    return HttpResponse('No')
    #  return render(request,'login.html') 

` if, I used hard coded value in authenticate method that i know saved in database username and password, then it will execute successful. I've debug form username and password They are coming. I tried to elaborate my problem. Please guide me how to solve this. Thank You. Screen Shot 1 Link: Having error in This Screen Shot 2 Link: But No error if Hard Coded Value

3
Can you try adding logs and check the value username and password during the request ?Kashyap KN

3 Answers

0
votes

Please remove the trailing commas from the username and password.

username = request.POST['username']
password = request.POST['password']

When you use a comma like this: elem = 2, in Python, you make it a tuple (2,), instead of a string that the authenticate function in your code requires.

0
votes
def loginAdmin(request):
   if request.method == "POST":
      username = request.POST['username']
      password = request.POST['password']
      user = auth.authenticate(username = username, password = password)
      if user is not None:
         auth.login(request, user)  
         return redirect('/admin-dashboard')
      else:
        messages.info(request, 'invalid')
        return redirect('/login')
  else:
      return HttpResponse('No')

Or You Can Write Like This:

from django.contrib.auth import authenticate, login
from django.http import HttpResponse, HttpResponseRedirect
def loginAdmin(request):
    if request.method == 'POST':
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            messages.success(request, 'Welcome Back')
            return HttpResponseRedirect(reverse('admin-dashboard'))
        else:
            messages.warning(request, 'Invalid Credentials')
            return HttpResponseRedirect(reverse('login'))
    else:
        if request.user.is_authenticated:
            return HttpResponseRedirect(reverse('admin-dashboard'))
        return render(request, 'dashboard/login.html')
0
votes
from django.contrib.auth import authenticate
from django.contrib.auth.models import User

username = request.POST['username']
password = request.POST['password']
user = authenticate(username = username, password = password)