zoukankan      html  css  js  c++  java
  • rbac组件之角色操作(二)

    为了与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

    操作页面

  • 相关阅读:
    local http
    redis 存储时间区间的数据
    json 和 jsonp
    ssdb 常用命令行
    php 冒泡排序
    pdo 函数
    异步的消息队列
    php 全局变量$_SERVER
    php 技能树
    get_called_class/get_class方法
  • 原文地址:https://www.cnblogs.com/shenjianping/p/10955994.html
Copyright © 2011-2022 走看看