def edit_view(request): '''Edit user info view, allows user to edit data represented on the main page''' user_info = UserInfo.objects.get_or_create(pk=1)[0] if request.method == 'GET': edit_form = EditUserInfoForm(instance=user_info) return render_to_response('edit.html', {'edit_form': edit_form}, context_instance=RequestContext(request)) edit_form = EditUserInfoForm(request.POST or None, request.FILES, instance=user_info) if edit_form.is_valid(): edit_form.save() return redirect(reverse('home')) else: return render_to_response( 'edit.html', {'edit_form': edit_form}, context_instance=RequestContext(request))
def edit_view_ajax(request): '''This view the same as edit view, but used only if edit form submited with ajax''' response = {} user_info = UserInfo.objects.get_or_create(pk=1)[0] edit_form = EditUserInfoForm( request.POST or None, request.FILES, instance=user_info) if edit_form.is_valid(): edit_form.save() response['request_result'] = 'Successfuly saved.' return HttpResponse( json.dumps(response), content_type='application/json') else: response = dict( [field, error] for field, error in edit_form.errors.items()) response['request_result'] = 'Error occurred' return HttpResponse( json.dumps(response), content_type='application/json')