0

Ok, so i want to update info about user with custom form that looks like this.

<form action="" >
 <!--csrftoken-->
  <input type="text" placeholder="Your new name" name="name">
  <input type="text" placeholder="Your new email" name="email">
  <input type="text" placeholder="Your new number" name="number">
  <button type="submit" class="raise">Submit</button>
</form>

How to update user with the data that was filled. Let's say user fills in the number and the email field and the rest was left blank, email and number are changed but the name was left untouched. I was thinking about form prepopulating, but this calls for another database request and i want to avoid that.

def updateCustomer(request):
    current           = request.user
    if request.method == 'POST':
        newphone      = request.POST['newPhone']
        newemail      = request.POST['email']
        newname       = request.POST['name']     
        addingToBase  = Customer.objects.get(user=current)
        xD            = addingToBase(email=newemail,name=newname,phone=newphone)
        xD.save()
        return redirect('home')

1 Answer 1

1

How about checking the returned values before assigning them?

def updateCustomer(request):
    current           = request.user
    if request.method == 'POST':
        newphone      = request.POST['number']
        newemail      = request.POST['email']
        newname       = request.POST['name']    
 
        curCust  = Customer.objects.get(user=current)

        if newphone:
            curCust.phone = newphone

        if newemail:
            curCust.email = newemail

        if newname:
            curCust.name = newname

        if newphone or newemail or newname:            
            curCust.save()

        return redirect('home')
Sign up to request clarification or add additional context in comments.

1 Comment

@Marcel, does that answer your question or did I misunderstand you? If so, please accept the answer as correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.