コード例 #1
0
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})
コード例 #2
0
 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)
コード例 #3
0
 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)
コード例 #4
0
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}))
コード例 #5
0
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)
コード例 #6
0
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)
コード例 #7
0
 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)
コード例 #8
0
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})
コード例 #9
0
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}))
コード例 #10
0
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)
コード例 #11
0
 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)