1 需求:django实现班级管理:查看(分页); 数据库采用django自带的sqlite3
2 数据库表创建
from django.db import models class Classes(models.Model): title = models.CharField(max_length=64) class Student(models.Model): name = models.CharField(max_length=64) age = models.IntegerField() email = models.EmailField() cls = models.ForeignKey('Classes') class Teacher(models.Model): name = models.CharField(max_length=64) age = models.IntegerField() email = models.EmailField() cls = models.ManyToManyField('Classes')
执行python manage.py makemigrations, python manage.py migrate使得数据库表创建生效
3 视图设计
# -*- coding: utf-8 -*- from django.shortcuts import render, redirect from app01 import models from django.forms import Form, fields class ClassForm(Form): title = fields.RegexField("^全栈w+") def classes(request): from utils.pagation_define import PageInfo current_page_number = request.GET.get('page') all_count = models.Classes.objects.all().count() page_info = PageInfo(current_page_number, all_count, "/app01/classes") class_list = models.Classes.objects.all()[page_info.start():page_info.end()] # 列表切片操作 a[start:end] start<= index < end return render(request, 'app01_class_list.html', {'class_list': class_list, 'page_info': page_info})
4 模板设计
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"> <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css"> </head> <body> <h4>班级管理</h4> <p> <a href="/app01/add_class" class="btn btn-primary">添加</a> </p> <table class="table table-striped table-bordered table-hover table-condensed"> <thead> <tr> <th>班级ID</th> <th>班级名称</th> <th>操作</th> </tr> </thead> <tbody> {% for item in class_list %} <tr> <td>{{ item.id }}</td> <td>{{ item.title }}</td> <td> <a href="/app01/edit_class/nid={{ item.id }}" class="glyphicon glyphicon-pencil">编辑</a>| <a href="/app01/del_class/nid={{ item.id }}" class="glyphicon glyphicon-trash">删除</a> </td> {#点击删除是一个get请求,要想告诉服务器id,可以通过url get请求获取,或者url匹配到传递给视图#} </tr> {% endfor %} </tbody> </table> <nav aria-label="Page navigation"> <ul class="pagination"> {{ page_info.pager|safe }} </ul> </nav> </body> </html>
5 补充分页模块
class PageInfo(object): def __init__(self, current_page_num, all_count, base_url, per_page=10, show_page=11): try: self.current_page_num = int(current_page_num) except Exception as e: self.current_page_num = 1 self.per_page = per_page a,b = divmod(all_count, per_page) if b: a = a + 1 self.all_page = a self.show_page = show_page self.base_url = base_url def start(self): return (self.current_page_num - 1) * self.per_page def end(self): return self.current_page_num * self.per_page def pager(self): page_list = [] # 如果总的页码数小于11 if self.show_page >= self.all_page: begin = 1 stop = self.all_page # 总页码数大于11 else: half,_ = divmod(self.show_page,2) if self.current_page_num <= half: begin = 1 stop = self.show_page else: temp = self.all_page - half if self.current_page_num >= temp: begin = self.all_page - self.show_page + 1 stop = self.all_page else: begin = self.current_page_num - half stop = self.current_page_num + half prev_page = '' if self.current_page_num == 1: prev_page = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">«</span></a></li>' else: prev_page = '<li><a href="%s?page=%s" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'% (self.base_url, self.current_page_num -1) page_list.append(prev_page) for i in range(begin, stop + 1): if i == self.current_page_num: temp = '<li class="active"><a href="%s?page=%s">%s</a></li>' % (self.base_url, i,i) else: temp = '<li><a href="%s?page=%s">%s</a></li>' % (self.base_url,i,i) page_list.append(temp) next_page= '' if self.current_page_num == self.all_page: next_page = '<li class="disabled"><a aria-label="Next"><span aria-hidden="true">»</span></a></li>' else: next_page = '<li><a href="%s?page=%s" aria-label="Next"><span aria-hidden="true">»</span></a></li>' % (self.base_url,self.current_page_num +1) page_list.append(next_page) return ''.join(page_list)