0

I am trying to create a simple login page. But user is always returned as none.

Here's my login.html

<form method = 'POST'>
        {% csrf_token %}
    <div class="body"></div>
        <div class="name">
            <div>Chili<span>Pili</span></div>
        </div>
        <br>
        <div class="login">
                <input type="text" placeholder="Username" name="username"><br>
                <input type="password" placeholder="Password" name="password"><br>
                <input type="submit" value="Login">
        </div>
    </form>

my login function in view.py

def login(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']

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

        if user is not None:
            login(user)
            print (user)
            messages.success(request, "You have successfully Logged In.")
            return redirect('index')
        else:
            messages.error(request, "You have entered invalid credentials. Please try again")
            return redirect('login')
    else:
        return render(request, 'main/login.html')
    

models.py:

class users(models.Model):
    _id = models.AutoField
    name = models.CharField(max_length = 100)
    username = models.CharField(max_length = 100)
    email = models.EmailField(max_length=254)
    hpassword = models.CharField(max_length = 255)
    category = models.CharField(max_length=50, default= "teacher")

    def __str__(self):
        return self.name 
        return self.username
        return self.email
        return self.category

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = [username, hpassword]

The keeps returning as none and only else statement gets executed. I am not able to understand what is going wrong here

SahilM
  • 118
  • 2
  • 13
  • Possible duplicates: https://stackoverflow.com/questions/18663083/django-authenticate-returns-none https://stackoverflow.com/questions/46619961/authenticate-returns-none-with-correct-username-and-password-during-userlogin/48971226 – ForceBru Jul 14 '20 at 13:03
  • Answers to those duplicate questions don't work for me unfortunately. – SahilM Jul 14 '20 at 13:30
  • @SahilM to extend django user, use BaseUserManager & AbstractBaseUser – Jaydeep Jul 14 '20 at 14:33
  • Okay. Thanks for suggestion. – SahilM Jul 14 '20 at 14:34
  • this might [help](https://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django) – Jaydeep Jul 14 '20 at 14:34
  • or you may use a user profile with OneToOne relation – Jaydeep Jul 14 '20 at 14:41
  • question is quite old to consider. Can you put an example with OneToOne relation in answer. I am not familiar with method. – SahilM Jul 14 '20 at 15:01

1 Answers1

0

try

def loginview(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(request, username=username, password=password)
    if user is not None:
        login(request, user)
        
        ...
    else:
       
        ...

the reason why you are getting that error is login(request,user) is recuring back to login view method rather thatn using auth login method.... change login view method name to "loginview" that will be fine.

  • method that I have used is also correct according to official documentation https://docs.djangoproject.com/en/3.0/topics/auth/default/#how-to-log-a-user-in – SahilM Jul 14 '20 at 13:25
  • The reason why you are getting that argument error is login(request,user) is recurring back to login view method rather than using auth login method.... change login view method name to "loginview" that will be fine. – ABDUL REHAN Jul 14 '20 at 13:46
  • 1
    I did change it but not helping because it is returning none. So code within if statement doesn't execute. – SahilM Jul 14 '20 at 13:51
  • did you alter authenticate method and include request as aurgument into it – ABDUL REHAN Jul 14 '20 at 13:54
  • There is no error it just gets None instead of value – SahilM Jul 14 '20 at 14:14