[Answer]-How to pass user through form

1👍

You may do this when you initialize the form:

phoneForm = PhoneForm({'user':request.user.id})

That way you’re passing the user who made the request to the form.

Here’s the full method:

def add_phone(request):
    user = request.user
    phoneForm = PhoneForm({'user':user.id})
    if request.method=='POST':        
        phoneForm = PhoneForm(request.POST) 
        if phoneForm.is_valid():
            phone=phoneForm.save(commit=False)
            phone.save()
        return redirect('/member/contact-list/')

Hope it helps.

0👍

Switching around some should fix your problem

def add_phone(request):
    if request.method == 'POST':        
        phoneForm = PhoneForm(request.POST)
        phoneForm.user = request.user #if a phone has a user and a form has a     
        if phoneForm.is_valid():
            phone = phoneForm.save(commit=False)
            phone.save()
        return redirect('/member/contact-list/')

I refactored a lot since you have some code that was redundant.
I removed most references to request.user

Leave a comment