菜单包括菜单列表,菜单列表不仅将菜单列出来,而且将每个菜单下的权限也列出来、菜单的添加、删除、修改。
urls.py
...re_path(r'^menus/list/$', MenuView.as_view(), name='menus_list'),re_path(r'^menus/add/$', MenuAddView.as_view(), name='menus_add'),re_path(r'^menus/edit/(?P
1、后台处理菜单列表页
class MenuView(View):def get(self,request):menu_queryset=Menu.objects.all()pid=request.GET.get('pid')if pid:root_permission_queryset = Permission.objects.filter(menu_id=pid).values('id', 'title', 'url', 'name', 'parent_id','menu__title')else:root_permission_queryset = Permission.objects.filter(menu_id__isnull=False).values('id', 'title', 'url', 'name', 'parent_id','menu__title')# print('permission_queryset',root_permission_queryset)
all_root_permission_queryset=Permission.objects.all().values('id', 'title', 'url', 'name', 'parent_id')root_permission_dict={}for row in root_permission_queryset:if not row['parent_id']:root_permission_dict[row['id']]={'id':row['id'],'title':row['title'],'name':row['name'],'url':row['url'],'menu_title':row['menu__title'],'children':[]}for row in all_root_permission_queryset:parent_id=row['parent_id']if parent_id in root_permission_dict:root_permission_dict[parent_id]['children'].append({'id':row['id'],'title':row['title'],'name':row['name'],'url':row['url']})print(root_permission_dict)return render(request,'rbac/menu_list.html',{'menu_queryset':menu_queryset,'root_permission_dict':root_permission_dict,'pid':pid})
将每一个菜单及其权限取出来,组成相应的数据结构
{64: {'id': 64,'name': 'rbac:multi_permissions','menu_title': '权限管理','title': '批量添加权限','children': [],'url': '/rbac/multi/permissions/'},1: {'id': 1,'name': 'stark:crm_userinfo_changelist','menu_title': '权限管理','title': '用户列表','children': [{'id': 2,'name': 'stark:crm_userinfo_add','title': '添加用户','url': '/stark/crm/userinfo/add/'}, {'id': 3,'name': 'stark:crm_userinfo_change','title': '修改用户','url': '/stark/crm/userinfo/(?P
}
2、处理添加菜单
添加以及修改使用的都是modelform
from django import forms
from rbac import models
from django.utils.safestring import mark_safeICON_LIST = [['fa fa-address-book', ''],['fa fa-address-book-o', ''],['fa fa-address-card', ''],['fa fa-address-card-o', ''],['fa fa-adjust', ''],['fa fa-american-sign-language-interpreting',''],['fa fa-stack-overflow', ''],['fa fa-clipboard', ''],]for item in ICON_LIST:item[1]=mark_safe(item[1])class MenuModelForm(forms.ModelForm):class Meta:model=models.Menufields=['title','icon']widgets = {'title': forms.TextInput(attrs={'placeholder': '请输入菜单名称', 'class': 'form-control'}),'icon':forms.RadioSelect(choices=ICON_LIST)}
class MenuAddView(View):def get(self,request):form = MenuModelForm()return render(request,'rbac/menu_add.html',{'form':form})def post(self,request):form=MenuModelForm(data=request.POST)if form.is_valid():form.save()return redirect(reverse('rbac:menus_list'))return render(request,'rbac/menu_add.html',{'form':form})
3、处理修改菜单
class MenuEditView(View):def get(self,request,mid):menu_obj=Menu.objects.filter(id=mid).first()if not menu_obj:return HttpResponse('该菜单不存在')form=MenuModelForm(instance=menu_obj)return render(request,'rbac/menu_edit.html',{'form':form})def post(self,request,mid):menu_obj=Menu.objects.filter(id=mid).first()form=MenuModelForm(data=request.POST,instance=menu_obj)if form.is_valid():form.save()return redirect(reverse('rbac:menus_list'))return render(request, 'rbac/menu_edit.html', {'form': form})
4、处理删除菜单
class MenuDelView(View):def get(self,request,mid):Menu.objects.filter(id=mid).first().delete()return redirect(reverse('rbac:menus_list'))