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 employee_add(request): if request.method == 'POST': form = EmployeeForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('employee')) else: form = EmployeeForm() context = {'form': form} return render(request, 'appTemp/employee/employee_add.html', context)
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 employee_edit(request, employee_id): if request.method == 'POST': employee = Employee.objects.get(pk=employee_id) form = EmployeeForm(request.POST, instance=employee) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('employee')) else: employee = Employee.objects.get(pk=employee_id) fields = model_to_dict(employee) form = EmployeeForm(initial=fields, instance=employee) context = { 'form': form, 'type': 'edit', } return render(request, 'appTemp/employee/employee_add.html', context)
def employee(request): if request.method == 'POST': # create a form instance and populate it with data from the request: form = EmployeeForm(request.POST) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required # some actions return HttpResponseRedirect(reverse('congrats')) return HttpResponseRedirect(reverse('fail')) # if a GET (or any other method) we'll create a blank form else: form = EmployeeForm() return render(request, 'emloyee.html', {'form': form})