0

I'm trying to log users in with django authenticate. It returns nothing even when the email and password are correct. It just reloads the page and doesn't do anything. I have checked and the email and password I used are stored in the database, so the account exist but login doesn't do anything. I am using django 2.

Views.py

def signup(request):
 if request.method == "POST":
    firstname = request.POST.get('firstname')
    lastname = request.POST.get('lastname')
    email = request.POST.get('email')
    password = request.POST.get('password')
    passwordagain = request.POST.get('passwordagain')
    areacode = request.POST.get('areacode')
    number = request.POST.get('number')
    userdetails = CustomUser(firstname=firstname,lastname=lastname,email=email,password=password, areacode=areacode, number=number)
    userdetails.save()
    return render(request, 'main/accountconfirmationpage.html')
else:
    return render(request,'main/signup.html')



def login(request):
    email=request.POST.get('email')
    password=request.POST.get('password')
    user = authenticate(request, email=email, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            return render(request,'main/dashboard.html')
    else:
        return render(request, 'main/login.html')



from django.db import models

Models.py

# Create your models here.
class CustomUser(models.Model):
    firstname = models.CharField(max_length=30)
    lastname = models.CharField(max_length=30)
    email = models.EmailField(max_length=30)
    password = models.CharField(max_length=100)
    areacode = models.CharField(max_length=4)
    number = models.CharField(max_length=30)
Ahtisham
  • 9,170
  • 4
  • 43
  • 57
Styxin
  • 115
  • 1
  • 9

2 Answers2

0

I think the problem is with your signup. You are not storing password properly in signup view.

userdetails = CustomUser(firstname=firstname,lastname=lastname,email=email,password=password, areacode=areacode, number=number)  # <-- Here
userdetails.save()

As you are not storing the password properly(currently it is being stored as plaintext rather than hashed), that is why authenticate function is not able to get the user.

So, you need to set password like this:

userdetails = CustomUser(firstname=firstname,lastname=lastname,email=email, areacode=areacode, number=number)
userdetails.set_password(password)
userdetails.save()

For more details, please check here on set_password function.

ruddra
  • 50,746
  • 7
  • 78
  • 101
  • I actually tried this before I posted this question, it didn't work. It says 'CustomUser' object has no attribute 'set_password'. I even tried changing CustomUser to django's User still no luck. – Styxin Jan 17 '19 at 05:58
  • to avail `set_password`, you need to subclass your CustomUser from [AbstractBaseUser](https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser) or [AbstractUser](https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#django.contrib.auth.models.AbstractUser). Hope it helps @Styxin – ruddra Jan 17 '19 at 06:04
0

You need to extend the Django User Model like this:

Models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

#this will extend User model with your own model
class CustomUser(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      # all the other fields you mentioned except areacode and number
      # are already in User model so don't need to add them here
      areacode = models.CharField(max_length=4)
      number = models.CharField(max_length=30)

# Below code will hook the create_custom_user and save_custom_user methods
# to the User model, whenever a save event occurs

@receiver(post_save, sender=User)
def create_custom_user(sender, instance, created, **kwargs):
    if created:
       CustomeUser.objects.create(user=instance)   

@receiver(post_save, sender=User):
def save_custom_user(sender, instance, **kwargs):
    instance.customeuser.save()

Now you can create the user with your CustomModel in views like this:

Views.py

from django.contrib.auth.models import User

def signup(request):
   if request.method == "POST":
      # I would suggest to use django forms 
      # instead of getting the values directly from request.POST  
      user = User(firstname=firstname, lastname=lastname, email=email)
      user.customuser.areacode = areacode
      user.customuser.number = number
      user.set_password(password)
      user.save()
      # rest of the code

doc for forms in django

Also for login the user with email instead of username you need to write an authentication backend for that and register it in your settings.py take a look here

Ahtisham
  • 9,170
  • 4
  • 43
  • 57