Reputation: 133
So i am stuck at a point in my project. I am trying to create users and I have extended User model using the OneToOnefield method as recommended in Documentation to create a user profile. There is no problem in creation of the user as all the details are correctly stored in both the auth_user and appname_userprofile table.
The problem I get is when I try to login with any user stored in the auth_user table. Except one case( which i created before creating the userProfile model). So in my auth_user Table the pass for this one case is encoded but for the remaining cases it's plain text.
Here is my function handling the view -
def login_view(request):
if request.method == 'POST':
print(request.body)
username = request.POST.get('id_username')
password = request.POST.get('id_password')
print(username, password)
try:
import pdb; pdb.set_trace()
user = authenticate(username=username, password=password)
print('This is ',user)
if user is not None:
login(request,user)
else:
return redirect('fileupload')
print('This is ',user).... some more code
Now when i pass the credentials for this one case and try to authenticate. the login works(except my code gets stuck at some later point because this user doesn't have any profile).
When i pass the credentials for user created after creating the UserProfile model. The
authenticate(username=username, password=password)
function returns
None
I have included this in my settings.py -
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
This is my model -
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
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
contact_no = models.CharField(max_length=10, null=True)
# email = models.EmailField()
department = models.CharField(max_length=25, null=True)
status = models.IntegerField(null=True)
industry_segment = models.CharField(max_length=50, null=True)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.IntegerField(null=True)
updated_at = models.DateTimeField(auto_now=True)
updated_by = models.IntegerField(null=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()
This is how i am saving the users -
def user_view(request):
print(request.user)
if request.is_ajax():
print(request.user)
data = json.load(request)
u_name = data['txtfirstName'] + " " + data['txtlastName']
user=User(username=u_name,password='test123',email=data['txtemailId'])
user.save()
# userprofile= UserProfile.objects.create(user=request.user)
user.userprofile.contact_no = data['txtcontactNo']
# user.userprofile.email = data['txtemailId']
user.userprofile.department = data['txtdeptvalue']
user.userprofile.status = data['txtstatusvalue']
user.userprofile.industry_segment = 'Software'
user.userprofile.created_at = datetime.datetime.now()
user.userprofile.created_by = request.user.id
user.userprofile.updated_at = datetime.datetime.now()
user.userprofile.updated_by = request.user.id
user.save()
ignore the formatting of this code... but it is working
So can anyone help me with the authentication problem ?
Upvotes: 0
Views: 351
Reputation: 599788
You must not create users like that. The password will not be hashed, so will never match in authenticate. You must always use the create_user
manager method.
user = User.objects.create_user(username=u_name, password='test123', email=data['txtemailId'])
Upvotes: 3