def user_edit(request, pk): """ 编辑 :param request: :return: """ origin_url = reverse("rbac:rbac_user_list") # 定义需要返回的地址 前端可以通过此地址跳转 obj = models.UserInfo.objects.filter(id=pk).first() # 数据是否存在 if not obj: return HttpResponse('用户不存在!') # 如果不存在,返回错误信息 if request.method == 'GET': form = UpdateUserModelForm(instance=obj) # 返回带有数据的页面 return render(request, 'rbac/user_change.html/', { 'form': form, "cancel": origin_url }) form = UpdateUserModelForm( instance=obj, data=request.POST) # POST表示修改了 一定要加instance=obj 否则就是增加了 if form.is_valid(): # 验证信息 form.save() return redirect(reverse("rbac:rbac_user_list")) # 成功后返回列表页面 return render(request, 'rbac/user_change.html/', { 'form': form, "cancel": origin_url }) # 否则返回错误信息
def user_edit(request, pk): #pk就是要修改的用户id obj = models.UserInfo.objects.filter(pk=pk).first() if not obj: return HttpResponse('用户不存在') if request.method == 'GET': form = UpdateUserModelForm(instance=obj) #这个instance就是可以传入默认值 return render(request, 'rbac/role_change.html', {'form': form}) form = UpdateUserModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:user_list')) return render(request, 'rbac/role_change.html', {'form': form})
def user_edit(request, id): obj = User.objects.filter(id=id).first() if not obj: return HttpResponse('不存在该用户') if request.method == 'GET': form = UpdateUserModelForm(instance=obj) return render(request, 'rbac/change.html', {'form': form}) form = UpdateUserModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:user_list')) return render(request, 'rbac/change.html', {'form': form})
def user_edit(request, pk): user_obj = models.UserInfo.objects.filter(id=pk).first() if request.method == 'GET': forms = UpdateUserModelForm(instance=user_obj) return render(request, 'rbac/change.html', {'forms': forms}) forms = UpdateUserModelForm(data=request.POST, instance=user_obj) if forms.is_valid(): forms.save() return redirect(reverse('rbac:user_list')) return render(request, 'rbac/change.html', {'forms': forms})
def user_edit(request, pk): """编辑用户""" obj = models.UserInfo.objects.filter(id=pk).first() if not obj: return HttpResponse('用户不存在') if request.method == 'GET': form = UpdateUserModelForm(instance=obj) return render(request, 'rbac/user_add.html', context=locals()) form = UpdateUserModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:user_list')) return render(request, 'rbac/user_add.html', context=locals())
def user_edit(request, pk): obj = models.UserInfo.objects.filter(id=pk).first() if not obj: return HttpResponse("用户不存在") if request.method == "GET": form = UpdateUserModelForm(instance=obj) return render(request, 'rbac/change.html', {"form": form}) form = UpdateUserModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:user_list')) return render(request, 'rbac/change.html', {"form": form})
def user_edit(request, id): """ 用户编辑功能视图函数 :param request: :param id: 当前用户的 id 号 :return: """ obj = models.UserInfo.objects.filter(uid=id).first() if not obj: return render(request, 'rbac/user_error.html') if request.method == 'GET': form = UpdateUserModelForm(instance=obj) # instance= 表示给模板传默认值 return render(request, 'rbac/change.html', {'form':form}) form = UpdateUserModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(reverse("user_list")) return render(request, 'rbac/change.html', {'form': form})
def user_edit(request, uid): """ 编辑用户 :param request: :param uid: 用户id :return: """ user = UserInfo.objects.filter(id=uid).first() if not user: return HttpResponse('数据不存在') if request.method == 'GET': form = UpdateUserModelForm(instance=user) return render(request, 'rbac/change.html', {'form': form}) form = UpdateUserModelForm(instance=user, data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:user-list')) return render(request, 'rbac/change.html', {'form': form})
def user_edit(request, pk): """ 编辑用户 :param request: :param pk:要修改的用户id :return: """ obj = models.UserInfo.objects.filter(id=pk).first() if not obj: return HttpResponse('用户不存在') if request.method == 'GET': form = UpdateUserModelForm(instance=obj) return render(request, 'rbac/change.html', {'form': form}) form = UpdateUserModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:user_list')) return render(request, 'rbac/change.html', {'form': form})
def user_edit(request, pk): ''' 编辑用户 :param request: pk:要修改的用户ID :return: ''' obj = models.UserInfo.objects.filter(id=pk).first() if not obj: # 当然这里404可以写一个页面,这里为了简单起见,所以直接返回 return HttpResponse("角色不存在") if request.method == 'GET': form = UpdateUserModelForm(instance=obj) return render(request, 'rbac/change.html', {'form': form}) form = UpdateUserModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:user_list')) return render(request, 'rbac/change.html', {'form': form})
def user_edit(request, pk): """ 编辑用户 :param request: :param pk: 要修改的用户ID :return: """ obj = models.UserInfo.objects.filter(id=pk).first() if not obj: return HttpResponse('用户不存在') if request.method == 'GET': form = UpdateUserModelForm(instance=obj) return render( request, 'rbac/change.html', { 'form': form, "users": request.session["show_permission"].get("user_name"), "rest_password": request.session.get("rest_password"), "user_edit_href": request.session.get("user_edit_href"), "queryset_count": request.session.get("queryset_count"), "queryset_count_all": request.session.get('queryset_count_all') }) form = UpdateUserModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:user_list')) return render( request, 'rbac/change.html', { 'form': form, "users": request.session["show_permission"].get("user_name"), "rest_password": request.session.get("rest_password"), "user_edit_href": request.session.get("user_edit_href"), "queryset_count": request.session.get("queryset_count"), "queryset_count_all": request.session.get('queryset_count_all') })
def user_edit(request, pk): ''' 编辑用户: 这里仅编辑用户名和邮箱,不编辑编码 此处重点:role_add, 与role_edit 显示的表单是一样的,唯一不同的是,role_add 页面表单不显示默认值,role_edit页面表单 显示默认值,因此,两个视频函数,添加role_add, 编辑role_edit 渲染时使用同一个页面文件role_add.html :param request: :param pk: 要修改的角色id :return: ''' obj = models.UserInfo.objects.filter(id=pk).first() if not obj: return HttpResponse('用户不存在') if request.method == 'GET': form = UpdateUserModelForm(instance=obj) #因为有instance=obj ,所以表单中有默认值 return render(request, 'rbac/change.html', {'form': form}) #访问http://127.0.0.1:8000/rbac/role/edit/1/, 表单中有默认值 form = UpdateUserModelForm(instance=obj, data=request.POST) #data=request.POST 表示提交过来的数据 if form.is_valid(): form.save() return redirect(reverse('rbac:user_list')) return render(request, 'rbac/change.html', {'form': form }) #错误信息的展示 防止表单没有输入直接点保存报错