创建数据库表
models.py
class Admin(models.Model): """管理员表""" username = models.CharField(verbose_name="用户名", max_length=32) password = models.CharField(verbose_name="密码", max_length=64)
在数据库生成
Tools->run manage.py task
执行命令
makemigrations migrate
数据库中插入一条数据
insert into app01_admin(username,password) values("coco", "123");
增加管理员菜单
修改/app01/templates/layout.html
<ul class="nav navbar-nav">
<li><a href="/admin/list/">管理员账户管理</a></li>
<li><a href="/depart/list/">部门管理</a></li>
<li><a href="/user/list/">用户管理</a></li>
<li><a href="/prettynum/list/">靓号管理</a></li>
</ul>
在views文件夹下新建admin.py,用于存放管理账户管理的视图函数
修改urls.py
#导入admin from app01.views import depart, pretty, user, admin #添加url urlpatterns = [ #管理员账户管理 path('admin/list/', admin.admin_list), ]
admin.py
from django.shortcuts import render
from app01 import models
def admin_list(request):
"""管理员列表"""
queryset = models.Admin.objects.all()
context = {
'queryset' : queryset
}
return render(request, 'admin_list.html', context)
在templates文件夹创建admin_list.html
{% extends 'layout.html' %} {% block content %} <div class="container"> <div style="margin-bottom: 10px" class="clearfix"> <a class="btn btn-success" href="#"> <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> 新建管理员 </a> <div style="float: right; width: 300px;"> <form> <div class="input-group"> <input type="text" name="q" class="form-control" placeholder="Search for..." value="{{ search_data }}"> <span class="input-group-btn"> <button class="btn btn-default" type="submit"> <span class="glyphicon glyphicon-search" aria-hidden="true"></span> </button> </span> </div><!-- /input-group --> </form> </div> </div> <div class="panel panel-default"> <!-- Default panel contents --> <div class="panel-heading"> <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 管理员列表 </div> <!-- Table --> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>用户名</th> <th>密码</th> <th>操作</th> </tr> </thead> <tbody> {% for obj in queryset %} <tr> <th>{{ obj.id }}</th> <td>{{ obj.username }}</td> <td>************</td> <td> <a class="btn btn-primary btn-xs" href="#">编辑</a> <a class="btn btn-danger btn-xs" href="#">删除</a> </td> </tr> {% endfor %} </tbody> </table> </div> </div> {% endblock %}
管理员列表就可以在前端展示出来了
管理员列表添加分页
admin.py
from django.shortcuts import render from app01 import models from app01.utils.pagination import Pagination def admin_list(request): """管理员列表""" queryset = models.Admin.objects.all() page_object = Pagination(request, queryset) context = { 'queryset' : page_object.page_queryset, 'page_string': page_object.html() } return render(request, 'admin_list.html', context)
admin_list.html
{% extends 'layout.html' %} {% block content %} <div class="container"> <div style="margin-bottom: 10px" class="clearfix"> <a class="btn btn-success" href="#"> <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> 新建管理员 </a> <div style="float: right; width: 300px;"> <form> <div class="input-group"> <input type="text" name="q" class="form-control" placeholder="Search for..." value="{{ search_data }}"> <span class="input-group-btn"> <button class="btn btn-default" type="submit"> <span class="glyphicon glyphicon-search" aria-hidden="true"></span> </button> </span> </div><!-- /input-group --> </form> </div> </div> <div class="panel panel-default"> <!-- Default panel contents --> <div class="panel-heading"> <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 管理员列表 </div> <!-- Table --> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>用户名</th> <th>密码</th> <th>操作</th> </tr> </thead> <tbody> {% for obj in queryset %} <tr> <th>{{ obj.id }}</th> <td>{{ obj.username }}</td> <td>************</td> <td> <a class="btn btn-primary btn-xs" href="#">编辑</a> <a class="btn btn-danger btn-xs" href="#">删除</a> </td> </tr> {% endfor %} </tbody> </table> </div> <ul class="pagination"> {{ page_string }} </ul> </div> {% endblock %}
完成
添加搜索功能
admin.py
from django.shortcuts import render from app01 import models from app01.utils.pagination import Pagination def admin_list(request): """管理员列表""" #构造搜索 data_dict = {} search_data = request.GET.get('q', "") if search_data: data_dict[ "username__contains"] = search_data #根据搜索条件去数据库获取 queryset = models.Admin.objects.filter(**data_dict) #分页 page_object = Pagination(request, queryset) context = { 'queryset' : page_object.page_queryset, 'page_string': page_object.html(), "search_data": search_data, } return render(request, 'admin_list.html', context)
admin_list.html
标签:管理,admin,list,queryset,html,管理员,page From: https://www.cnblogs.com/kekeeleven/p/17774382.html