Django项⽬实战开发之点餐系统第四章员⼯信息管理之分页显⽰+内容搜索
本章内容是针对上⼀章内容进⾏⼀个升级,即加上了⼀个内容搜索功能
1、视图⽂件myadmin/views/myadmin_users.py
from django.shortcuts import render
dels import user
from django.http import HttpResponse
paginator import Paginator
from dels import Q
# Create your views here.
# 加载后台员⼯管理⾸页
def myadmin_users_index(request,Pindex):
try:
ulist = user.objects.filter(status__lt=9)
django项目实例
kw = ('keyword',None)#查询关键字
mywhere =[]
if kw:
ulist = user.objects.filter(Q(username__contains=kw)|Q(nickname__contains=kw))#通过账号或昵称搜索
mywhere.append('keyword='+ kw)
PageUsers = Paginator(ulist,5)#(实例化对象)5个数据分⼀页
Pagenum = PageUsers.num_pages #最⼤页数
PageRange = PageUsers.page_range #页数范围
if Pindex <1:
Pindex =1
elif Pindex > Pagenum:
Pindex = Pagenum
PageContext = PageUsers.page(Pindex)#每页的内容
context ={"users":PageContext,"Pindex":Pindex,"PageRange":PageRange,"kw":mywhere} return render(request,"myadmin/user/myadmin_users_index.html",context)
except Exception as err:
print(err)
return HttpResponse("⽆员⼯数据")
2、模板⽂件templates/user/myadmin_users_index.html
提交搜索form表单,⽤GET⽅法请求
在之前分页跳转超链接基础上再添加请求条件,防⽌冲突
整体代码为以下
{% extends 'myadmin/base.html'%}
{% block main_content %}
<section class="content-header">
<h1>
员⼯管理
<small>订餐系统后台管理</small>
</h1>
<ol class="breadcrumb">
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i>⾸页</a></li>
<li class="active">员⼯管理</li>
</ol>
</section>
<!-- Main content -->
<section class="content container-fluid">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">员⼯信息表</h3>
<div class="box-tools">
<form action="{% url 'myadmin_users_index' Pindex %}" method="GET">
<div class="input-group input-group-sm" >
<input type="text" name="keyword"class="form-control pull-right" placeholder="Search">
<div class="input-group-btn">
<button type="submit"class="btn btn-default"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
</div>
</div>
<!--/.box-header -->
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
<th>ID</th>
<th>账号</th>
<th>昵称</th>
<th>当前状态</th>
<th>添加时间</th>
<th>修改时间</th>
<th width="200">操作</th>
</tr>
{%for user in users %}
<tr>
<td>{{user.id}}</td>
<td>{{user.username}}</td>
<td>{{user.nickname}}</td>
<td>{%if user.status ==1%}<span class="label label-success">正常</span>
{%elif user.status ==2%}<span class="label label-success">禁⽤</span>
{%elif user.status ==6%}<span class="label label-success">管理员</span>
{%elif user.status ==9%}<span class="label label-success">删除</span>
{%else%}<span class="label label-success">未知</span>
{% endif %}</td>
<td>{{ate_at}}</td>
<td>{{user.update_at}}</td>
<td>
<button type="button"class="btn btn-success btn-xs">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span>编辑</button>
<button type="button"class="btn btn-danger btn-xs">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>删除</button>
<button type="button"class="btn btn-warning btn-xs">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>查看订单</button> </td>
</tr>
{% endfor %}
</table>
</div>
<!--/.box-body -->
<div class="box-footer clearfix">
<div class="box-footer clearfix">
<ul class="pagination pagination-sm no-margin pull-right">
<li><a href="{% url 'myadmin_users_index' Pindex|add:-1 %}?{{kw|join:'&'}}">«</a></li>
{%for Page in PageRange %}
<li {%if Page == Pindex %}class='active'{% endif %}><a href="{% url 'myadmin_users_index' Page %}?{{kw|join:'&'}}">{{Page}}</a></li> {% endfor %}
<li><a href="{% url 'myadmin_users_index' Pindex|add:1 %}?{{kw|join:'&'}}">»</a></li>
</ul>
</div>
</div>
<!--/.box -->
</div>
</div>
</section>
{% endblock %}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。