def ajax_personal_info_form(request): if not request.is_ajax() and request.method != 'POST': return HttpResponseRedirect('/personal-info-form/') # prepare the forms try: profile = Profile.objects.get(id=request.user.pk) except ObjectDoesNotExist: profile = None user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=profile) # validate form data if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() # respond new_pic = False if profile.picture: new_pic = profile.picture.url response_dict = {'saved': True, 'picture': new_pic, } json = simplejson.dumps(response_dict) return HttpResponse(json) else: # inform the caller about errors response_dict = {'saved': False, 'errors': profile_form.errors} json = simplejson.dumps(response_dict) return HttpResponse(json)
def personal_info_form(request): try: profile = Profile.objects.get(id=request.user.pk) except ObjectDoesNotExist: profile = None if request.method == "POST": user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() return HttpResponseRedirect('/personal-info-form/') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=profile) return{'user_form': user_form, 'profile_form': profile_form, }