添加
添加一个
@blue.route('/add/',methods=['GET','POST'])
def stu_add():
if request.method == 'GET':
return render_template('add.html')
if request.method == 'POST':
# 获得数据
username = request.form.get('username')
phone = request.form.get('phone')
age = request.form.get('age')
# 保存数据
stu = Student()
stu.s_name = username
stu.s_age = age
stu.s_phone = phone
# 会话保存
db.session.add(stu)
db.session.commit()
return redirect(url_for('app.list'))
添加多个
@blue.route('/add_all/',methods=['GET','POST'])
def add_all():
stus=[]
for _ in range(10):
stu = Student()
stu.s_age = random.randint(18,28)
stu.s_name = '小明%s' % random.randint(0,1000)
stu.s_phone = '1234567890'
stus.append(stu)
db.session.add_all(stus)
db.session.commit()
return 'done'
编辑
@blue.route('/edit/<int:id>/',methods=['GET','POST'])
def stu_edit(id):
if request.method == 'GET':
stu=Student.query.filter(Student.id == id).first()
return render_template('add.html', stu=stu)
if request.method == 'POST':
# 1.获得页面中的参数
username = request.form.get('username')
phone = request.form.get('phone')
age = request.form.get('age')
# 2.获得对象
stu = Student.query.filter(Student.id == id).first()
# 3.修改属性
stu.s_name = username
stu.s_age = age
stu.s_phone = phone
db.session.add(stu)
db.session.commit()
return redirect(url_for('app.list'))
@blue.route('/change_stu/',methods=['GET'])
def change_stu():
stu = Student.query.filter(Student.id == 3).first()
stu.s_name = '彼得帕克'
#db.session.add(stu) 可写可不写
db.session.commit()
return 'done'
删除
@blue.route('/del/<int:id>/',methods=['GET','POST'])
def stu_del(id):
stu=Student.query.filter(Student.id==id).first()
db.session.delete(stu)
db.session.commit()
return redirect(url_for('app.list'))
查询
@blue.route('/sel_stu/',methods=['GET'])
def sel_stu():
# 查询id=3的信息 三种方式
stu = Student.query.filter(Student.id == 3).first()
stu = Student.query.filter_by(id=3).first()
stu = Student.query.get(3)
# 查询所有数据 返回的是列表
suts = Student.query.all()
# 排序
suts = Student.query.order_by('s_age')
suts = Student.query.order_by('s_age asc')
# 降序
suts = Student.query.order_by('-s_age')
suts = Student.query.order_by('s_age desc')
# 实现分页
stus = Student.query.offset(0).limit(2)
# 模糊查询
# contains 包含某个字段
stus = Student.query.filter(Student.s_name.contains('帕克')).all()
names = [stu.s_name for stu in stus]
print(names)
# 以小开头的姓名
stus = Student.query.filter(Student.s_name.startswith('小')).all()
# 以3结束的姓名
stus = Student.query.filter(Student.s_name.endswith('3')).all()
# 第二位是'明'的姓名
stus = Student.query.filter(Student.s_name.like('_明%')).all()
# id是3,4,5
stus = Student.query.filter(Student.id.in_([3,4,5]))
names = [stu.s_name for stu in stus]
# 查询年龄小于21的信息
stus = Student.query.filter(Student.s_age.__le__(21)).all()
# 查询年龄小于22的信息且姓名以2结束
stus = Student.query.filter(Student.s_age<22).filter(Student.s_name.endswith('2')).all()
stus = Student.query.filter(Student.s_age<22,Student.s_name.endswith('2')).all()
# or_
stus = Student.query.filter(or_(Student.s_age < 22, Student.s_name.endswith('2'))).all()
分页
# 列表
@blue.route('/list/',methods=['GET','POST'])
def list():
#students=Student.query.all()
# 127.0.0.1:8080/app/list/?page=3
page = int(request.args.get('page',1))
pre_page = 5
#如果page是1,pre_page是5的话 显示的就是第一页的5条数据
#如果page是2,pre_page是5的话 显示的就是第二页的5条数据
paginate = Student.query.paginate(page,pre_page)
# items获得生成器的具体对象
students = paginate.items
return render_template('list.html',students=students,
paginate=paginate)
分页的页面
{% extends 'base.html' %}
{% block title %}
学生列表页面
{% endblock %}
{% block content %}
<p><a href="{{url_for('app.stu_add')}}">添加学生信息</a></p>
<table>
<thead>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>电话</th>
</thead>
<tbody>
{% for stu in students %}
<tr>
<td>{{stu.id}}</td>
<td>{{stu.s_name}}</td>
<td>{{stu.s_age}}</td>
<td>{{stu.s_phone}}</td>
<td>
<a href="{{url_for('app.stu_edit',id=stu.id)}}">编辑</a>
|
<a href="{{url_for('app.stu_del',id=stu.id)}}">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<p>
{% if paginate.has_prev %}
<a href="{{url_for('app.list')}}?page={{paginate.prev_num}}">上一页</a>
{% endif %}
{% for i in paginate.iter_pages() %}
<a href="{{url_for('app.list')}}?page={{i}}">{{i}}</a>
{% endfor %}
{% if paginate.has_next %}
<a href="{{url_for('app.list')}}?page={{paginate.next_num}}">下一页</a>
{% endif %}
当前{{paginate.page}}页,共{{paginate.pages}}页,一共{{paginate.total}}数据
</p>
{% endblock %}
网友评论