def host_add(request): if request.method == 'GET': form = HostModelForm() return render(request, 'rbac/change.html', {'form': form}) form = HostModelForm(data=request.POST) if form.is_valid(): form.save() return redirect(memory_reverse(request, 'host_list')) return render(request, 'rbac/change.html', {'form': form})
def host_edit(request, pk): obj = models.Host.objects.filter(id=pk).first() if not obj: return HttpResponse('主机不存在') if request.method == 'GET': form = HostModelForm(instance=obj) return render(request, 'rbac/change.html', {'form': form}) form = HostModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(memory_reverse(request, 'host_list')) return render(request, 'rbac/change.html', {'form': form})
def host_add(request): if request.method == 'GET': form = HostModelForm() print('form:', form) return render(request, 'rbac/change.html', {'form': form}) form = HostModelForm(data=request.POST) if form.is_valid(): form.save() return redirect(memory_reverse( request, 'host_list')) #反向生成 改成 memory_reverse ,去掉rbac: return render(request, 'rbac/change.html', {'form': form}) #防止表单没有输入直接点保存报错
def host_add(request): """ 添加主机 :param request: :return: """ if request.method == "GET": form = HostModelForm() return render(request, "rbac/change.html", {'form': form}) form = HostModelForm(data=request.POST) if form.is_valid(): form.save() return redirect(memory_reverse(request, 'host_list')) return render(request, "rbac/change.html", {'form': form})
def host_edit(request, pk): """ 编辑主机 :param request: :param pk: 要修改主机的ID :return: """ obj = models.UserInfo.objects.filter(pk=pk).first() if not obj: return HttpResponse("主机不存在") if request.method == "GET": form = HostModelForm(instance=obj) return render(request, "rbac/change.html", {'form': form}) form = HostModelForm(instance=obj, data=request.POST) if form.is_valid(): form.save() return redirect(memory_reverse(request, 'host_list'))
def host_edit(request, pk): obj = models.Host.objects.filter(id=pk).first() if not obj: return HttpResponse('主机不存在') if request.method == 'GET': form = HostModelForm(instance=obj) #因为有instance=obj ,所以表单中有默认值 return render(request, 'rbac/change.html', {'form': form }) #访问http://127.0.0.1:8000/rbac/role/edit/1/, 表单中有默认值 form = HostModelForm(instance=obj, data=request.POST) #data=request.POST 表示提交过来的数据 if form.is_valid(): form.save() return redirect(memory_reverse( request, 'host_list')) #改成 memory_reverse ,去掉rbac: return render(request, 'rbac/change.html', {'form': form}) #错误信息的展示 防止表单没有输入直接点保存报错