Reputation: 2482
What I basically want to accomplish is that when a user edits his profile, a blank field causes everything to be overwritten.
For example:
if a first name is added and the user wants t change it he wont have to refill the whole form, the only field he will have to change is the first name field.When the form gets submitted the other blank fields cause their model field to be empty.
here is what i tried:
def editUserprofile(request):
rc = context_instance=RequestContext(request)
u = request.user
if request.method=='POST':
form = UserProfileEdit(request.POST, request.FILES)
if form.is_valid():
u = UserProfile.objects.get(user=u)
if 'avatar' in request.FILES :
u.avatar = request.FILES['avatar']
if 'first_name' in request.POST:
u.first_name = form.cleaned_data['first_name']
if 'last_name' in request.POST:
u.last_name = form.cleaned_data['last_name']
if 'email' in request.POST:
u.email = form.cleaned_data['email']
if 'country' in request.POST:
u.country = form.cleaned_data['country']
if 'date_of_birth' in request.POST:
u.date_of_birth = form.cleaned_data['date_of_birth']
u.save()
return HttpResponseRedirect(reverse('photocomp.apps.users.views.editUserprofile'))
else:
u = UserProfile.objects.get(user=u)
form = UserProfileEdit()
return render_to_response('users/editprofile.html',
{'form':form, 'u':u},
rc)
Can I someway make the empty field pass without editing my models content?
Hope it doesn't confuse you so much!
If you want more detail just tell me :) thanks in advance!
Upvotes: 0
Views: 83
Reputation: 10177
Since this is edit form, you should consider pre-filling the fields with the current values. This is easy by submitting the constructor an instance
argument (if you are using ModelForm). This would pre-fill the values with the correct values and the user can change whichever he/she wants.
form = UserProfileEdit(instance=u)
Upvotes: 1