标签:选择 cts data rms hid style dde objects lis
###############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 %}
rbac05 权限角色管理 (modelform/反向生成url/共用删除,编辑页面/共用确认,取消页面)
标签:选择 cts data rms hid style dde objects lis
原文地址:https://www.cnblogs.com/Jnhnsnow/p/11670665.html