djangodjango-authenticationdjango-usersdjango-login

User doesn't exist problem in login page of django User model


I have created one simple recipe website. In which i am creating a login page and register page. The register page is working fine and it saving the user credential into the User model. but when i have created the login page. it showing that username doesn't exist but the user is registered with same username and password.

I have tried all the query like using filter and get separately..but didn't get any desired result.

I expect that when user type their user name and password it redirect to recipe page. if the credentials are wrong it redirect to login page

def login_page(request):
    if request.method == "POST":
        username = request.POST['username']
        print(username)
        password = request.POST['password']
        # print(User.objects.filter(username=username))
               
        user = authenticate(username = username, password = password)
        if user is None:
            messages.info(request, "user do not exit. please REGISTER")
            return redirect('/user_login/')
        else:
            login(request, user)
            return redirect('/receipes/')
               
    return render(request, 'login_page.html')


def register_page(request):
    try:
        if request.method == "POST":
            username = request.POST['username'],
            first_name = request.POST['first_name'],
            last_name = request.POST['last_name']
    
            if User.objects.filter(username=username):
                messages.info(
                    request, "Username already exist! Please try some other username.")
                return redirect('/user_register/')
    
            user = User.objects.create(
                username=username, first_name=first_name, last_name=last_name
            )
            user.set_password(request.POST['password'])
            user.save()
            messages.success(
                request, "Your Account has been created succesfully!!"
            )
            return redirect('/user_register/')
    except:
        pass

    return render(request, 'register_page.html')

Solution

  • I replicated your project with the code you provided and I believe that I've solved your issue.


    The TL;DR version is in your register_page function, remove the trailing commas after each variable like so

    Before

    username = request.POST['username'],
    first_name = request.POST['first_name'],
    last_name = request.POST['last_name']
    

    After

    username = request.POST['username']
    first_name = request.POST['first_name']
    last_name = request.POST['last_name']
    

    If you're interested in what steps I took to figure it out, please read on because I didn't notice this at first.


    First, I created a clean Django project and setup your code with as minimal setup as I could, to make sure that nothing else could possibly interfere with it

    Second, I tried to duplicate the problem exactly as you described and had the same issue where I couldn't login to a successfully registered user.

    That prompted me to check the django admin (/admin/) to make sure that the user was succesfully created. So I created a new superuser with py manage.py createsuperuser command and checked the User table.

    Immediately I noticed some red flags, the user I tried to register through your view had this username: ('test', ), this first name: ('tester firstname', ) and this last name: tester lastname. So that's told me that something was happening when the username and firstname were being processed in your view.

    Finally that lead me back to check the code to your view - where I noticed the trailing commas, removed them and everything worked perfectly.