码迷,mamicode.com
首页 > 其他好文 > 详细

rbac组件之角色操作(二)

时间:2019-06-01 09:41:28      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:objects   sel   margin   self   title   import   需要   layout   closed   

为了与stark组件分离,形成独立的模块,所以rbac数据表的操作需要单独进行操作,对角色表的操作。

urls.py

urlpatterns = [
    re_path(r^roles/list/$, RoleView.as_view(),name=roles_list),
    re_path(r^roles/add/$, RoleAddView.as_view(), name=roles_add),
    re_path(r^roles/edit/(?P<rid>\d+)/$, RoleEditView.as_view(), name=roles_edit),
    re_path(r^roles/dell/(?P<rid>\d+)/$, RoleDelView.as_view(), name=roles_del),
]

views.py

from django.shortcuts import render,redirect,HttpResponse
from django.views import View
from rbac.models import *
from rbac.forms.roles import RoleModelForm
from django.urls import reverse

# Create your views here.

class RoleView(View):
    def get(self,request):
        role_queryset=Role.objects.all()
        return render(request,rbac/role_list.html,{role_queryset:role_queryset})

class RoleAddView(View):
    def get(self,request):
        form = RoleModelForm()
        return render(request,rbac/role_add.html,{form:form})

    def post(self,request):
        form=RoleModelForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse(rbac:roles_list))
        return render(request,rbac/role_add.html,{form:form})

class RoleEditView(View):

    def get(self,request,rid):
        role_obj=Role.objects.filter(id=rid).first()
        if not role_obj:
            return HttpResponse(该角色不存在)
        form=RoleModelForm(instance=role_obj)
        return render(request,rbac/role_edit.html,{form:form})

    def post(self,request,rid):
        role_obj=Role.objects.filter(id=rid).first()
        form=RoleModelForm(data=request.POST,instance=role_obj)
        if form.is_valid():
            form.save()
            return redirect(reverse(rbac:roles_list))
        return render(request, rbac/role_edit.html, {form: form})

class RoleDelView(View):

    def  get(self,request,rid):
        Role.objects.filter(id=rid).first().delete()
        return redirect(reverse(rbac:roles_list))

role_list.html

技术图片
{% extends ‘layout.html‘ %}
{% block css %}


{% endblock %}
{% block content %}
    <div class="role_list">
    <div class="container">
        <a href="{% url ‘rbac:roles_add‘ %}" class="btn btn-primary add_role">添加角色</a>
        <table class="table table-hover table-bordered">
            <thead>
            <tr>
                <td>角色ID</td>
                <td>角色名称</td>
                <td>权限个数</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>
            {% for row in role_queryset %}
            <tr>
                <td>{{ row.id }}</td>
                <td>{{ row.title }}</td>
                <td>{{ row.permissions.count }}</td>
                <td>
                    <a href="{% url ‘rbac:roles_edit‘ rid=row.id %}"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                     <span style="padding: 2px 2px;display: inline-block">|</span>
                    <a href="{% url ‘rbac:roles_del‘ rid=row.id %}"><i class="fa fa-trash" aria-hidden="true"></i></a>
                </td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
    </div>
{% endblock %}
View Code

role_add.html

技术图片
{% extends ‘layout.html‘ %}

{% block content %}
    <div style="margin-top: 20px">
    <form  method="post" class="form-horizontal" novalidate>
    {% csrf_token %}
    {% for field in form %}
     <div class="row">
    <div class="form-group">
    <label class="col-sm-2 control-label">{{ field.label }}</label>
    <div class="col-sm-6">
        {{ field }}<span style="color: red;display: inline-block">{{ field.errors.0 }}</span>
    </div>
  </div>
     </div>
     {% endfor %}
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-primary">提交</button>
    </div>
  </div>
    </form>
     </div>
{% endblock %}
View Code

role_edit.html

技术图片
{% extends ‘layout.html‘ %}

{% block content %}
    <div style="margin-top: 20px">
    <form method="post" class="form-horizontal" novalidate>
    {% csrf_token %}
     <div class="row">
    <div class="form-group">
    <label class="col-sm-2 control-label">角色名称</label>
    <div class="col-sm-6">
        {{ form.title }}<span style="color: red;display: inline-block">{{ form.title.errors.0 }}</span>
    </div>
  </div>
     </div>
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-primary">提交</button>
    </div>
  </div>



    </form>
     </div>
{% endblock %}
View Code

 

操作页面

技术图片

 

rbac组件之角色操作(二)

标签:objects   sel   margin   self   title   import   需要   layout   closed   

原文地址:https://www.cnblogs.com/shenjianping/p/10955994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!