Reputation: 43
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')
Upvotes: 0
Views: 115
Reputation: 178
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')
Upvotes: 1