(1). 会员信息数据表:member
- 在数据库
osdb
中创建member
表,若此表已存在请跳过
CREATE TABLE `member` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '会员表id',
`nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
`avatar` varchar(255) DEFAULT NULL COMMENT '头像',
`mobile` varchar(50) DEFAULT NULL COMMENT '电话',
`status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '状态:1正常/2禁用/9删除',
`create_at` datetime DEFAULT NULL COMMENT '添加时间',
`update_at` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
(2). 定义模型Model类
- 进入myadmin应用目录中,编辑:
myobject/myadmin/models.py
模型文件,添加如下代码
from django.db import models
from datetime import datetime
#会员信息模型
class Member(models.Model):
nickname = models.CharField(max_length=50) #昵称
avatar = models.CharField(max_length=255) #头像
mobile = models.CharField(max_length=50) #电话
status = models.IntegerField(default=1) #状态:1正常/2禁用/9删除
create_at = models.DateTimeField(default=datetime.now) #创建时间
update_at = models.DateTimeField(default=datetime.now) #修改时间
def toDict(self):
return {'id':self.id,'nickname':self.nickname,'avatar':self.avatar,'mobile':self.mobile,'status':self.status,'create_at':self.create_at.strftime('%Y-%m-%d %H:%M:%S'),'update_at':self.update_at.strftime('%Y-%m-%d %H:%M:%S')}
class Meta:
db_table = "member" # 更改表名
(3). 项目urls路由信息配置
- 打开根路由文件:myobject/myadmin/urls.py路由文件,编辑路由配置信息
from django.urls import path
from myadmin.views import member
urlpatterns = [
...
#会员管理路由
path('member/<int:pIndex>', member.index, name="myadmin_member_index"), #浏览会员
]
(4). 编辑视图文件
- 新建视图文件:myobject/myadmin/views/member.py 视图文件,并进行编辑
#会员信息视图文件
from django.shortcuts import render
from django.http import HttpResponse
from django.core.paginator import Paginator
from myadmin.models import Member
# ==============后台会员信息管理======================
# 浏览会员信息
def index(request,pIndex=1):
mod = Member.objects
list = mod.filter(status__lt=9)
#执行分页处理
pIndex = int(pIndex)
page = Paginator(list,5) #以5条每页创建分页对象
maxpages = page.num_pages #最大页数
#判断页数是否越界
if pIndex > maxpages:
pIndex = maxpages
if pIndex < 1:
pIndex = 1
list2 = page.page(pIndex) #当前页数据
plist = page.page_range #页码数列表
#封装信息加载模板输出
context = {"memberlist":list2,'plist':plist,'pIndex':pIndex,'maxpages':maxpages}
return render(request,"myadmin/member/index.html",context)
(5). 编写模板文件
- 5.1. 打开父类模板:/templates/myadmin/base.html ,编辑导航栏代码
...
<li><a href="{% url 'myadmin_member_index' 1 %}"><i class="fa fa-users"></i> <span>会员管理</span></a></li>
...
- 5.2. 后台会员信息浏览页模板:/templates/myadmin/member/index.html
{% extends 'myadmin/base.html' %}
{% load static from staticfiles %}
{% block main_body %}
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
会员管理
<small>订餐系统后台管理</small>
</h1>
<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">
<div class="input-group input-group-sm" style="width: 150px;">
<input type="text" name="table_search" 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>
</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>修改时间</th>
<th width="200">操作</th>
</tr>
{% for vo in memberlist %}
<tr>
<td>{{ vo.id }}</td>
<td>{{ vo.nickname }}</td>
<td>{{ vo.avatar }}</td>
<td><img src="/static/uploads/member/{{ vo.avatar }}" width="40"/></td>
<td>{{ vo.mobile }}</td>
<td>
{% if vo.status == 1 %}
<span style="color:green">正常</span>
{% elif vo.status == 2 %}
<span style="color:red">禁停</span>
{% elif vo.status == 9 %}
<span style="color:red">已删除</span>
{% else %}
<span style="color:red">未知状态</span>
{% endif %}
</td>
<td width="15%">{{ vo.create_at|date:'Y-m-d H:i:s' }}</td>
<td width="15%">{{ vo.update_at|date:'Y-m-d H:i:s' }}</td>
<td width="22%">
<a href="#" class="btn btn-success btn-xs">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span> 编辑</a>
<button type="button" class="btn btn-danger btn-xs">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除</button>
</td>
</tr>
{% endfor %}
</table>
</div>
<!-- /.box-body -->
<div class="box-footer clearfix">
<ul class="pagination pagination-sm no-margin pull-right">
<li><a href="{% url 'myadmin_member_index' pIndex|add:-1 %}">«</a></li>
{% for p in plist %}
<li {% if p == pIndex %}class="active"{% endif %}><a href="{% url 'myadmin_member_index' p %}">{{p}}</a></li>
{% endfor %}
<li><a href="{% url 'myadmin_member_index' pIndex|add:1 %}">»</a></li>
</ul>
</div>
</div>
<!-- /.box -->
</div>
</div>
</section>
<!-- /.content -->
{% endblock %}
(6). 运行测试
[root@localhost myobject]# pwd
/python/myobject
[root@localhost myobject]# ls
manage.py myadmin myobject myweb static templates
[root@localhost myobject]# python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
April 08, 2018 - 12:20:30
Django version 1.11, using settings 'myobject.settings'
Starting development server at http://0:8000/
Quit the server with CONTROL-C.
^C[root@localhost myobject]#