def form_profile_ajax(request): if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): cd = form.cleaned_data try: user_profile = request.user.get_profile() except UserProfile.DoesNotExist: ##create profile user_profile = UserProfile(user=request.user) ##update profile user_profile.country = cd['country'] user_profile.about = cd['about'] user_profile.save() ##update User User.objects.filter(username=request.user.username).update(first_name = cd['first_name'], last_name = cd['last_name'], email = cd['email'] ) #a cool thing to return the html generated by rendering a template in Json: #html = render_to_string(template, {'review': review }) ##can also use: if request.is_ajax() to be sure that javascript is allowed return HttpResponse(json.dumps({'form_ok':True, 'form_uncomplete':any(map(lambda x: x=='', cd.values())), 'html':ProfileForm(cd).as_table()}), mimetype='application/json') else: #form not valid, we resubmit it with error msg so that the user correct it return HttpResponse(json.dumps({'form_ok':False, 'form_uncomplete':False, 'html':form.as_table()}), mimetype='application/json') else: ##user is not authentified return HttpResponseRedirect('/profile')