def user_login(request): try: if request.method == "POST": form = UserForm(request.POST) print(form.is_valid()) if form.is_valid(): username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user: if user.is_active: login(request, user) variable = User.objects.filter( username=username).values('id') for a in variable: request.session['id'] = a['id'] global simple_variable simple_variable = request.session['id'] print(simple_variable) return redirect("/dashboard") else: message = "Unable to login. Either username or password is incorrect." return render(request, 'login.html', { 'form': form, 'message': message }) else: return render(request, 'login.html', {'form': form}) else: form = UserForm() return render(request, 'login.html', {'form': form}) except Exception as e: db_logger.exception(e) return redirect("/error")
def employee_add(request): if request.method == 'POST': uf = UserForm(request.POST) if uf.is_valid(): uf.save() return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'employee/add.html', {'uf': uf}) else: uf = UserForm() return render(request, 'employee/add.html', {'uf': uf})
def employee_edit(request, id=None): user = get_object_or_404(User, id=id) if request.method == 'POST': user_form = UserForm(request.POST, instance=user) if user_form.is_valid(): user_form.save() return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'employee/edit.html', {"user_form": user_form}) else: user_form = UserForm(instance=user) return render(request, 'employee/edit.html', {"user_form": user_form})
def EmployeeAdd(request): if request.method == 'POST': user_form = UserForm(request.POST) if user_form.is_valid(): user_form.save() return HttpResponseRedirect(reverse('employee-list')) else: return render(request, 'employee/add.html', {'user_form': user_form}) else: user_form = UserForm() return render(request, 'employee/add.html', {'user_form': user_form})
def employee_edit(request, id): u = get_object_or_404(User, id=id) if request.method == 'POST': uf = UserForm(request.POST, instance=u) if uf.is_valid(): uf.save() return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'employee/edit.html', {'uf': uf}) else: uf = UserForm(instance=u) return render(request, 'employee/edit.html', {'uf': uf})
def employee_add(request): context = {} if request.method == 'POST': user_form = UserForm(request.POST) context['user_form'] = user_form if user_form.is_valid(): user_form.save() return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'employee/add.html', context) else: user_form = UserForm() return render(request, 'employee/add.html', context)
def employee_edit(request,id=None): user=get_object_or_404(User,id=id) context={} if request.method=='POST': user_form=UserForm(request.POST,instance=user) context['user_form']=user_form if user_form.is_valid(): user_form.save() return redirect('employee_list') else: return render(request,'employee/edit.html',context) else: user_form=UserForm(instance=user) context['user_form']=user_form return render(request,'employee/edit.html',context)
def employee_edit(request, id=None): user = get_object_or_404(User, pk=id) if request.method == 'POST': # passing instance=user means you have to update the data # else it is going to create a new row user_form = UserForm(request.POST, instance=user) if user_form.is_valid(): user_form.save() return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'employee/edit.html', {"user_form": user_form}) else: user_form = UserForm(instance=user) return render(request, 'employee/edit.html', {"user_form": user_form})
def employee_add(request): if request.method == 'POST': user_form = UserForm(request.POST) if user_form.is_valid(): user_form.save() return HttpResponseRedirect(reverse('employee_list')) # reverse method takes a perameter # which is the matching name field in the urls.py # when it finds the name field it convert that into url # Here, reverse('employee_list') -> employee/ else: return render(request, 'employee/add.html', {"user_form": user_form}) else: user_form = UserForm() return render(request, 'employee/add.html', {"user_form": user_form})
def employee_add(request): #if request.role == "Admin": context = {} if request.method == 'POST': user_form = UserForm(request.POST) context['user_form'] = user_form if user_form.is_valid(): u = user_form.save() messages.success(request, 'New Employee has been added successfully') return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'employee/add.html', context) else: user_form = UserForm() context['user_form'] = user_form return render(request, 'employee/add.html', context)
def employee_add(request): context = {} # if request.role=='Admin': if request.method == "POST": user_form = UserForm(request.POST) context['user_form'] = user_form if user_form.is_valid(): user_form.save() return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'employees/add.html/', context) else: user_form = UserForm() context['user_form'] = user_form return render(request, 'employees/add.html/', context)
def employee_add(request): if request.method == 'POST': user_form = UserForm(request.POST) if user_form.is_valid(): user_form.save() return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'employee/add.html', {'user_form': user_form}) else: user_form = UserForm() context = { 'user_form': user_form, 'title': "Employee", } return render(request, 'employee/add.html', context)
def employee_detail(request, pk=None): employee = Employee.objects.get(id=pk) user = User.objects.get(id=employee.user_id) if request.method == 'POST': # form = EmployeeForm(request.POST, request.FILES) employee_form = EmployeeForm(request.POST, instance=employee) user_form = UserForm(request.POST, instance=user) if user_form.is_valid() and employee_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() employee = employee_form.save(commit=False) employee.user = user if request.FILES: employee.image = request.FILES['image'] employee.save() # return HttpResponseRedirect('/employee/') return redirect('employee_detail', pk) else: print(request.POST.get('password')) print(user_form.errors) else: print('not') user_form = UserForm(instance=user) employee_form = EmployeeForm(instance=employee) user_image = 'https://www.w3schools.com/howto/img_avatar.png' if employee.image: user_image = employee.image.url context = { "add": False, "employee": employee, "form": employee_form, 'user_form': user_form, "user_image": user_image, } return render(request, "employee_detail.html", context)
def employee_add(request): if request.method == 'POST': user_form = UserForm(data=request.POST) employee_form = EmployeeForm(data=request.POST) if user_form.is_valid() and employee_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() employee = employee_form.save(commit=False) employee.user = user if request.FILES: employee.image = request.FILES['image'] employee.save() return redirect('employee_index') else: user_form = UserForm() employee_form = EmployeeForm() context = { "add": True, 'user_form': user_form, "form": employee_form, 'user_image': 'https://www.w3schools.com/howto/img_avatar.png' } return render(request, "employee_add.html", context)