角色列表添加角色(forms.ModelForm):
###############forms.ModelForm##############
角色列表编辑角色:
编辑和添加角色可以共用一个模板 ====》 change.html
def role_edit(request,pk): obj = models.Role.objects.filter(pk=pk).first() if not obj: return HttpResponse("角色不存在") if request.method == "GET": print(pk) form = RoleModelForm(instance=obj) ###########传入obj对象 则input value带默认值############ return render(request, ‘rbac/change.html‘, {‘form‘:form,‘title‘:‘编辑角色‘}) form = RoleModelForm(data=request.POST,instance=obj) if form.is_valid(): form.save() return redirect(reverse(‘rbac:role_list‘)) return render(request, ‘rbac/change.html‘, {‘form‘:form,‘title‘:‘编辑角色‘})
{% extends ‘layout.html‘ %} {% block content %} <h3>{{ title }}</h3> <div class="luffy-container"> <form class="form-horizontal" method="post" novalidate> {% csrf_token %} {% for fields in form %} <div class="form-group"> <label class="col-sm-2 control-label"> {{ fields.label }} </label> <div class="col-sm-8"> {{ fields }} <span style="color: red;"> {{ fields.errors.0 }}</span> </div> </div> {% endfor %} <div class="form-group"> <div class="col-sm-offset-2 col-sm-8"> <input type="submit" value="保存" class="btn btn-primary"> </div> </div> </form> </div> {% endblock %}
角色列表删除角色: ######所有视图共用一个确认/取消页面的方法#######
跳转页面 让用户选择是否确认:
def role_del(request,pk): origin_url = reverse(‘rbac:role_list‘) ############## 主页面############## 变量 根据视图变 if request.method == ‘GET‘: return render(request,‘rbac/delete.html‘,{‘cancel‘:origin_url})############ models.Role.objects.filter(id=pk).delete() return redirect(origin_url)#############跳转回主页面##########
{% extends ‘layout.html‘ %} {% block content %} <div class="luffy-container"> <div class="alert alert-danger" role="alert"> <form method="post">##############到 视图的post请求 {% csrf_token %} <p style="font-size: 13px"> <i class="fa fa-waring" aria-hidden="true">删除后不可恢复,是否确认删除</i> </p> <div style="margin-top: 20px"> <a href="{{ cancel }}" class="btn btn-default btn-sm">取消</a> ####################跳转回主页面 origin_url = reverse(‘rbac:role_list‘)
<input type="submit" class="btn btn-danger btn-sm" value="确认"> </div> </form> </div> </div> {% endblock %}
原文地址:https://www.cnblogs.com/Jnhnsnow/p/11670665.html
时间: 2024-10-31 08:31:30