Search code examples
mysqldjango-modelsdjango-authentication

Getting Error "type object 'User' has no attribute 'POST' " while login authentication while fetching data from Mysql database


I am stuck here from past 2days. It worked fine when first time I created "createsuperuser" command and I next I modified code by authenticating only Email and Password without username. Again I created newdatabase in Mysql and created "superuser". I also created Regestration method with Posts data into database it is working fine. But when I fetch data(Loin Authentication) it is throwing error. And I dont have any Form models in my App.

from django.contrib.auth.models import User, auth
from django.contrib import messages


def view_comein(request):
    if request.method == 'POST':
    username = User.POST['username']
    # it is not executing here
    password = User.POST['password']

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

    if user is not None:
        auth.loginPage(user)
        return redirect('/')
    else:
        messages.info(request, 'Invalied Credentials')
        return redirect('loginPage')


 else:
     return render(request, 'loginPage.html')

Error in browser


Solution

  • Please look closely below code. You are accessing invalid attribute.

    def view_comein(request):
        if request.method == 'POST':
            username = request.POST['username'] #**request; not User**
            # it is not executing here
            password = request.POST['password']
            user = auth.authenticate(username = username, password = password)
    
            if user is not None:
                auth.login(request, user) #auth.login; not auth.loginPage