def update(request, id): employee = Employee.objects.get(id=id) form = EmployeeForm(request.POST, instance=employee) if form.is_valid(): form.save() return redirect("/") return render(request, 'edit.html', {'employee': employee})
def put(self,request,*args,**kwargs): data=request.body if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'please send valid json'}),status=400) pdata=json.loads(request.body) id=pdata.get('id',None) if id is None: return self.render_to_http_response(json.dumps({'msg':'please send me id '}),status=400) emp=self.get_object_by_id(id) if emp is None: return self.render_to_http_response(json.dumps({'msg':'Object Does not DoesNotExistq1'}),status=400) original_data={ 'eno':emp.eno, 'ename':emp.ename, 'esal':emp.esal, 'passw':emp.passw, 'rpass':emp.rpass } original_data.update(pdata) form=EmployeeForm(original_data,instance=emp) if form.is_valid(): form.save(commit=True) return self.render_to_http_response(json.dumps({'msg':"Record updated successfully"})) if form.errors: json_data=json.dumps(form.errors) return self.render_to_http_response(json_data,status=400)
def put(self, request, *args, **kwargs): data = request.body if not is_json(data): return self.render_to_http_response(json.dumps( {'msg': 'pleas send me json data'}), status=400) pdata = json.loads(request.body) id = pdata.get('id', None) if id is None: return self.render_to_http_response(json.dumps( {'msg': "Id is required"}), status=400) obj = self.get_object_by_id(id) if obj is None: json_data = json.dumps({'msg': 'Object not exist'}) return self.render_to_http_response(json_data, status=404) new_data = pdata old_data = { 'eno': obj.eno, 'ename': obj.ename, 'esal': obj.esal, 'eaddr': obj.eaddr, } old_data.update(new_data) form = EmployeeForm(old_data, instance=obj) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg': 'updated successfully'}) return self.render_to_http_response(json_data, status=201) if form.errors: json_data = json.dumps(form.errors) return self.render_to_http_response(json_data, status=400)
def update(request, id): employee = Employee.objects.get(id=id) form = EmployeeForm(request.GET, instance=employee) if form.is_valid(): form.save() return redirect("/show") temp = loader.get_template('edit.html') return HttpResponse(temp.render({'employees': id}))
def insert_view(request): e=EmployeeForm() if request.method=="POST": f=EmployeeForm(request.POST) if f.is_valid(): f.save(commit=True) return redirect("/") d={'form':e} return render(request,'myapp/insert.html',d)
def update_view(request,id): e=Employee.objects.get(id=id) if request.method=="POST": f=EmployeeForm(request.POST,instance=e) if f.is_valid(): f.save(commit=True) return redirect("/") d={'emp':e} return render(request,'myapp/update.html',d)
def post(self,request,*args,**kwargs): data=request.body if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'please send valid json'}),status=400) pdata=json.loads(request.body) form=EmployeeForm(pdata) if form.is_valid(): form.save(commit=True) return self.render_to_http_response(json.dumps({'msg':'employee save successfully'}),status=200) if form.error: json_data=json.dumps(form.errors) return self.render_to_http_response(json_data,status=400)
def addnew(request): if request.method == "POST": form = EmployeeForm(request.POST) if form.is_valid(): try: form.save() return redirect('/') except: pass else: form = EmployeeForm() return render(request, 'index.html', {'form': form})
def emp(request): if request.method == "GET": form = EmployeeForm(request.GET) if form.is_valid(): try: form.save() return redirect('/show') except: pass else: form = EmployeeForm() temp = loader.get_template('index.html') return HttpResponse(temp.render({'form': form}))
def Employee_form(request): if request.method == "POST": form = EmployeeForm(request.POST or None, request.FILES or None) if form.is_valid(): obj = form.save(commit=False) obj.save() return HttpResponseRedirect('/') else: form = EmployeeForm() context = {'form': form} return render(request, 'myapp/Employee_form.html', context)
def post(self, request, *args, **kwargs): data = request.body if not is_json(data): return self.render_to_http_response(json.dumps( {'msg': 'pls send me json_data only'}), status=400) empdata = json.loads(request.body) print(empdata) form = EmployeeForm(empdata) if form.is_valid(): obj = form.save(commit=True) return self.render_to_http_response( json.dumps({'msg': 'resource created successfully'})) if form.errors: json_data = json.dumps(form.errors) return self.render_to_http_response(json_data, status=400)