权限管理功能已经完整实现了,本节来做一些基本的优化配置,包括添加面包导航和系统入口页面的一些内容。
1、增加面包屑导航
随着项目进展,系统功能页面会越来越多,如果在每个功能模板页中都去添加面包屑导航,这样做实在是太笨拙了,我们通过获取用户访问的url信息,从数据库中查询url实例,然后在功能页模板继承的父模板中通过实例来渲染面包屑导航。
1.1 视图配置
1、几乎每个功能主页都需要用到面包屑导航,所以我们写一个mixin类用来获取当前访问URL实例,并添加到上下文中,打开sandboxMP/apps/custom.py,添加如下内容:
from system.models import Menu
class BreadcrumbMixin:
def get_context_data(self, **kwargs):
menu = Menu.get_menu_by_request_url(url=self.request.path_info)
if menu is not None:
kwargs.update(menu)
return super().get_context_data(**kwargs)
2、先从system应用系统访入口开始,打开sandboxMP/apps/system/views.py,修改SystemView:
from django.views.generic import TemplateView
from .mixin import LoginRequiredMixin
from custom import BreadcrumbMixin
class SystemView(LoginRequiredMixin, BreadcrumbMixin, TemplateView):
template_name = 'system/system_index.html'
3、打开sandboxMP/apps/system/views_menu.py 修改MenuListView:
from apps.custom import BreadcrumbMixin
class MenuListView(LoginRequiredMixin, BreadcrumbMixin, ListView):
model = Menu
context_object_name = 'menu_all'
3、打开sandboxMP/apps/system/views_role.py 修改RoleView:
from apps.custom import BreadcrumbMixin
class RoleView(LoginRequiredMixin, BreadcrumbMixin, TemplateView):
template_name = 'system/role.html'
4、打开sandboxMP/apps/system/views_structure.py 修改StructureView:
from apps.custom import BreadcrumbMixin
class StructureView(LoginRequiredMixin, BreadcrumbMixin, TemplateView):
template_name = 'system/structure/structure.html'
5、打开sandboxMP/apps/system/views_user.py 修改UserView:
from apps.custom import BreadcrumbMixin
class UserView(LoginRequiredMixin, BreadcrumbMixin, TemplateView):
template_name = 'system/users/user.html'
1.2 模板配置
打开sandboxMP/templates/base-left.html,在模板最下面{% block content %} 标签上面添加如下内容:
<!-- 注意这段内容是加在<div class="content-wrapper">下面,{% block content %}的上面 -->
<section class="content-header margin-bottom">
<ol class="breadcrumb">
{% if menu.parent %}
<li class="active"><a href="{{ menu.parent.url | default:'' }}">{{ menu.parent.name }}</a></li>
{% endif %}
<li class="active"><a href="{{ menu.url }}">{{ menu.name }}</a></li>
</ol>
</section>
完成以上配置,运行项目,访问各个功能页面,就可以看到右上角的面包屑导航。
2 完善系统入口模板页
系统模块(system)入口页是一个空白页面,我们来写入一些静态内容,完善入口页面。具体内容你可以自己选择。
删除sandboxMP/templates/system/system_index.html里面全部内容,替换成下面内容:
{% extends "base-left.html" %}
{% load staticfiles %}
{% block content %}
<div class="pad margin no-print">
<div class="callout callout-info" style="margin-bottom: 0!important;">
<h4><i class="fa fa-info-circle"></i>系统管理:</h4>
系统管理模块包含:系统权限管理和系统设置,系统权限管理可实现基于角色组的权限管理,可根据角色组权限动态生成URL导航菜单。
</div>
</div>
<!-- Main content -->
<section class="invoice">
<div class="row">
<div class="col-xs-12">
<h2 class="page-header">
<i class="fa fa-github"></i> RBAC权限管理历史版本
</h2>
</div>
<!-- /.col -->
</div>
<div class="row">
<div class="col-xs-12 table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>编号</th>
<th>TAG</th>
<th>发布日期</th>
<th>TAG地址</th>
<th>Commit</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>v1.19</td>
<td>2018-11-16</td>
<td>https://github.com/RobbieHan/sandboxMP/tree/v1.19</td>
<td>system config</td>
</tr>
<tr>
<td>2</td>
<td>v1.18</td>
<td>2018-11-16</td>
<td>https://github.com/RobbieHan/sandboxMP/tree/v1.18</td>
<td>rbac config</td>
</tr>
<tr>
<td>3</td>
<td>v1.17</td>
<td>2018-11-14</td>
<td>https://github.com/RobbieHan/sandboxMP/tree/v1.17</td>
<td>role2menu</td>
</tr>
<tr>
<td>4</td>
<td>v1.16</td>
<td>2018-11-14</td>
<td>https://github.com/RobbieHan/sandboxMP/tree/v1.17</td>
<td>role2user</td>
</tr>
</tbody>
</table>
</div>
<!-- /.col -->
</div>
<!-- /.row -->
<div class="row">
<!-- accepted payments column -->
<div class="col-xs-12">
<p class="lead">其他信息:</p>
<p class="text-muted well well-sm no-shadow" style="margin-top: 10px;">
<strong>权限管理开发文档获取地址(知识星球):</strong> https://t.zsxq.com/a6IqBMr (微信中打开链接)<br>
<strong>知识星球快捷入口:</strong>微信公众号搜索[知识星球],关注后发送52824366,获取星球连接。<br>
<strong>知乎专栏SandBox:</strong>https://zhuanlan.zhihu.com/sandbox <br>
<strong>轻量级办公管理系统项目开源地址:</strong>https://github.com/RobbieHan/gistandard <br>
</p>
</div>
<!-- /.col -->
</div>
</section>
<!-- /.content -->
<div class="clearfix"></div>
{% endblock %}
{% block javascripts %}
{% endblock %}
3 添加requirements文件
给项目添加requirements文件,用来记录项目开发和部署所需要的依赖环境。
在项目根目录创建一个新的文件夹,名称叫做:requirements,在目录中创建两个文件dev.txt和pro.txt,在文件中写入项目现在已经安装的依赖包:
django==2.1.2
pillow==5.3.0
4 设置登录超时时间
在sandboxMP/sandboxMP/settings.py中添加如下配置:
# session timeout
SESSION_COOKIE_AGE = 60 * 20 # session有效时间为20分钟
SESSION_EXPIRE_AT_BROWSER_CLOSE = True # 关闭浏览器Cookie失效
SESSION_SAVE_EVERY_REQUEST = True # 以上两个配置需要配合这一条才能够生效
至此基于角色组的权限管理功能全部完结。你可以尝试在权限管理模块的基础上扩展自己的app。
最新最全文档,请关注我的知识星球: https:// t.zsxq.com/a6IqBMr (微信中打开链接)
知识星球快捷入口:微信公众号搜索:知识星球,关注后发送52824366,获取星球连接。
本节文档对应源码版本: https:// github.com/RobbieHan/sa ndboxMP/tree/v1.19
文档配套项目地址: https:// github.com/RobbieHan/sa ndboxMP
知乎专栏SandBox: https:// zhuanlan.zhihu.com/sand box
轻量级办公管理系统项目开源地址: https:// github.com/RobbieHan/gi standard