Skip to content Skip to sidebar Skip to footer

How Can I Update First_name, Last_name & Email Of Django Model User And Update Other Field In Separate Models

I have been learning the Django tutorial for a few weeks using YT, StackOverflow, Django documentation, and other sources. So, maybe my question goes out of the sense. I hope you w

Solution 1:

You can simply pass the user_id in the context to the template and then use it.


Solution 2:

class Account(models.Model):
    user = models.OneToOneField(User, null=True, on_delete=models.SET_NULL)
    profile_photo = models.ImageField(upload_to='profile', null=True)
    mobile_number = models.PositiveBigIntegerField(null=True)
    date_of_birth = models.DateField(auto_now_add=False, auto_now=False, null= 
    True)
    zip_code = models.IntegerField(null=True)
    address = models.CharField(max_length=225, null=True)
    website = models.URLField(max_length=100, null=True)
    bio = models.CharField(max_length=225, null=True)

def profileSettingsPage(request):
    form1 = AccountForm()
    form2 = CreateUserForm()
    if request.method == 'POST':
        instance_user = User.objects.get(pk=request.user.id)
        instance_account = Account.objects.get(user=instance_user)
        form1 = AccountForm(request.POST, request.FILES,instance=instance_account)
        form2 = CreateUserForm(request.POST,instance=instance_user)

        if form1.is_valid() and form2.is_valid():
            form2.save()
            form1.save()
            messages.success(request, 'Profile saved.')
            return redirect('profile-settings')
        else:
            print(form1.errors)
            print(form2.errors)
            messages.warning(request, 'Faile to saved profile')
            return redirect('profile-settings')
    context = {'form1':form1, 'form2':form2}
    return render(request, 'profile-settings.html', context)

Post a Comment for "How Can I Update First_name, Last_name & Email Of Django Model User And Update Other Field In Separate Models"