def search(request): users = User.objects.all() if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): user = form.save() return HttpResponseRedirect(reverse('search')) else: form = UserForm() return render(request, 'search.html', { 'users': users, 'form': form } )
def edit(request, id): user = get_object_or_404(User, id=id) initial = { 'firstName': user.firstName, 'lastName': user.lastName, 'email': user.email } if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): user = form.save(user=user) return HttpResponseRedirect(reverse('settings')) else: form = UserForm(initial=initial) return render(request, 'edit.html', { 'user': user, 'form': form } )
def edit_user(request): user = get_object_or_404(UserMy, pk=request.user.id) context = {} if request.method == 'POST' and request.user.is_authenticated(): form = UserForm(request.POST, instance=user) if form.is_valid(): user = form.save() context['success'] = True else: form = UserForm(instance=user) context['form'] = form print(context) return render(request, 'edit_user.html', context)