def update(req, id): employee = Employee.objects.get(id=id) form = EmployeeForm(req.POST, instance=employee) if form.is_valid(): form.save() return redirect("/show") return render(req, "crudapp/edit.html", {"employee": employee})
def update(request, id): employee = Employee.objects.get(id=id) form = EmployeeForm(request.POST, instance=employee) if form.is_valid(): form.save() return redirect('/show') return render(request, "edit.html", {'employee': employee})
def put(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg':'Please send valid json data..'}) return self.return_to_http_response(json_data, status=404) pydata = json.loads(data) id = pydata.get('id',None) if id is None: json_data = json.dumps({'msg':'To perform updata id is mandatory, please provide id..'}) return self.return_to_http_response(json_data, status=400) emp = self.get_object_by_id(id) if emp is None: json_data = json.dumps({'msg':'No resource with matched id, not possible to perform updation..'}) return self.return_to_http_response(json_data,status=404) provided_data = json.loads(data) original_data = { 'eno':emp.eno, 'ename':emp.ename, 'esal':emp.esal, 'eaddr':emp.eaddr, } original_data.update(provided_data) form = EmployeeForm(original_data, instance=emp) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg':'Resource updated Successfully'}) return self.return_to_http_response(json_data) if form.errors: return self.return_to_http_response(json_data, status=400)
def put(self, request, *args, id, **kwargs): emp = self.get_object_by_id(id=id) if emp == None: json_data = json.dumps({'msg':'No matching id found. Unable to Update..'}) return self.return_to_http_response(json_data, status=404) data = request.body # data contain json data which came from client app valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg':'Please send valid json data..'}) return self.return_to_http_response(json_data, status=404) provided_data = json.loads(data) # provided data contain python object which is given by client app # original_data means which is stored in database and which will be updated original_data = { 'eno': emp.eno, 'ename': emp.ename, 'esal': emp.esal, 'eaddr': emp.eaddr, } original_data.update(provided_data) form = EmployeeForm(original_data, instance=emp) '''instance=emp : it means - the client provided_data will stored in the existing place(object). It doesn't create second object to store the new provided_data''' if form.is_valid(): form.save() json_data = json.dumps({'msg':'Data Updated Successfully'}) return self.return_to_http_response(json_data) if form.errors: json_data = json.dumps(form.errors) return self.return_to_http_response(json_data, status=404)
def update(request, id): employee = Employee.objects.get(id=id) form = EmployeeForm(request.POST) if form.is_valid(): form.save() return redirect("/show") return render(request, 'edit.html', {'employee': employee})
def update_view(request, id): employee = Employee.objects.get(id=id) if request.method == 'POST': form = EmployeeForm(request.POST, instance=employee) if form.is_valid(): form.save() return redirect('/') return render(request, 'crudapp/update.html', {'employee': employee})
def update_view(request, id): employee = Employee.objects.get(id=id) my_dict = {'employee': employee} if request.method == "POST": form = EmployeeForm(request.POST, instance=employee) if form.is_valid(): form.save() return redirect('/display/') return render(request=request, template_name='crudapp/update.html', context=my_dict)
def create_view(request): form = EmployeeForm() if request.method == 'POST': form = EmployeeForm(request.POST) if form.is_valid(): form.save() return redirect('/') return render(request, 'crudapp/create.html', {'form': form})
def post(self, request, *args, **kwargs): # json_data = json.dumps({'msg':'This is from post request'}) # return self.return_to_http_response(json_data) ''' The below line is taking data from client application''' data = request.body # request.body receive the data from client application which is send by test.py valid_json = is_json(data) # This is_json method is written in utils.py if not valid_json: json_data=json.dumps({'msg':'please send valid json data only'}) return self.return_to_http_response(json_data, status=400) emp_data = json.loads(data) form = EmployeeForm(emp_data) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg':'Resource created successfully'}) return self.return_to_http_response(json_data) if form.errors: json_data = json.dumps(form.errors) return self.return_to_http_response(json_data, status=400)
def emp(req): if req.method == "POST": form = EmployeeForm(req.POST) if form.is_valid(): try: form.save() return redirect("/show") except: pass else: form = EmployeeForm() return render(req, "crudapp/index.html", {"form": form})
def create_data(request): form = EmployeeForm() my_dict = {'form': form} if request.method == 'POST': form = EmployeeForm(request.POST) if form.is_valid(): form.save() return redirect('/display/') return render(request=request, template_name='crudapp/create.html', context=my_dict)
def emp(request): if request.method == "POST": form = EmployeeForm(request.POST) if form.is_valid(): try: form.save() return redirect('/show') except: pass else: form = EmployeeForm() return render(request, 'index.html', {'form': form})
def emp(request): if request.method == "POST": form = EmployeeForm(request.POST) if form.is_valid(): # if valid then save try: form.save() return redirect("/show") except: pass else: form = EmployeeForm() return render(request, "index.html", {'form': form})