Django实战1-权限管理功能实现-10:用户管理
这节将会介绍用户管理功能的实现,包括用户信息浏览、添加、删除和修改等操作,从这一节开始很多功能实现都是和前面组织架构管理功能实现类似,所以通过这一节我们将完整实现用户管理功能。
1 用户列表展示
为了能够在列表中展示所有的用户信息,我们需要写两个视图,一个是用来提供模板页的基础视图,另外一个是用来访问用户数据列表的接口视图,然后通过使用ajax将数据组合填充到datatables中进行展示。
1.1 视图配置
打开sandboxMP/apps/system/views_user.py, 添加如下内容
import json
from django.views.generic.base import TemplateView
from django.shortcuts import HttpResponse
from django.contrib.auth import get_user_model
User = get_user_model()
class UserView(LoginRequiredMixin, TemplateView):
template_name = 'system/users/user.html'
class UserListView(LoginRequiredMixin, View):
def get(self, request):
fields = ['id', 'name', 'gender', 'mobile', 'email', 'department__name', 'post', 'superior__name', 'is_active']
ret = dict(data=list(User.objects.values(*fields)))
return HttpResponse(json.dumps(ret), content_type='application/json')
知识点介绍:
1、UserView:继承了TemplateView基本类视图,通过template_name指定返回的模板页
2、UserListView:通过QuerySet的values方法来获取指定字段列的数据内容,转换QuerySet类型最终序列化成json串,返回数据访问接口
3、User = get_user_model():使用自定义用户模型的方法
4、department__name:departent是外键,默认存储在数据库里面的是department_id, 使用这种写法可以直接通过QuerySet的values方法获取department的name值,类似的还有superior__name
1.2 URL配置
打开 sandboxMP/apps/system/urls.py, 添加新的url:
from . import views_user
app_name = 'system'
urlpatterns = [
...原有内容省略...
path('basic/user/', views_user.UserView.as_view(), name='basic-user'),
path('basic/user/list/', views_user.UserListView.as_view(), name='basic-user-list'),
]
# 完成以上配置第一个url : http://127.0.0.1:8000/system/basic/user/是会报错的,
# 错误信息:TemplageDoesNotExist,因为我肯还没有配置模板;
# 第二个URL是可以访问的:http://127.0.0.1:8000/system/basic/user/list/
# 因为UserListView返回的是一个json数据接口
1.3 模板配置
复制sandboxMP/template/index.html 文件到sandboxMP/template/system/users/目录下,重命名为user.html, 并修改内容如下:
{% extends "base-left.html" %}
{% load staticfiles %}
{% block css %}
{% endblock %}
{% block content %}
<!-- Main content -->
<section class="content">
当前访问页:用户管理, 这里是用户管理的基本页,用户管理所有模板页内容将会在这里添加
</section>
<!-- /.content -->
{% endblock %}
{% block javascripts %}
{% endblock %}
这时候就可以访问我们的用户管理页面了:http://127.0.0.1:8000/system/basic/user/
接下来继续修改我们的user.html模板页,使用datatables来展示我们的用户数列表
1、修改user.html文件,删除section标签中刚才添加的临时内容,然后在section标签中写入下面的内容:
<div id="devlist">
<div class="box box-primary" id="liebiao">
<div class="box-header">
<div class="btn-group pull-left">
<button type="button" id="btnRefresh" class="btn btn-default">
<i class="glyphicon glyphicon-repeat"></i>刷新
</button>
</div>
<div class="btn-group pull-left"> </div>
<div class="btn-group pull-left">
<button type="button" id="btnCreate" class="btn btn-default">
<i class="glyphicon glyphicon-plus"></i>新增
</button>
</div>
<div class="btn-group pull-left"> </div>
<div class="btn-group pull-left">
<button type="button" id="btnDelete" class="btn btn-default">
<i class="glyphicon glyphicon-trash"></i>删除
</button>
</div>
<div class="btn-group pull-left"> </div>
<div class="btn-group pull-left">
<button type="button" id="btnEnable" class="btn btn-default">
<i class="glyphicon glyphicon-ok-circle"></i>启用
</button>
<button type="button" id="btnDisable" class="btn btn-default">
<i class="glyphicon glyphicon-ban-circle"></i>禁用
</button>
</div>
<div class="btn-group pull-right">
<form class="form-inline">
<div class="form-group">
<label>用户状态:</label>
<select id="select" name="select" class="form-control">
<option style='text-align:center' value=''>-----所有-----</option>
<option value="True">启用</option>
<option value="False">禁用</option>
</select>
</div>
</form>
</div>
</div>
<div class="box-body">
<table id="dtbList" class="display" cellspacing="0" width="100%">
<thead>
<tr valign="middle">
<th><input type="checkbox" id="checkAll"></th>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<