Flask 前端分页与下载

Flask 前端分页与下载

Flask 前端分页与下载

1
2
3
4
5
6
7
8
9
{% if goods.has_prev %}
<a href="/show?page={{ goods.prev_num }}">上一页</a>
{% endif %}
{% for foo in goods.iter_pages() %}
<a href="/show?page={{ foo }}">{{ foo }}</a>
{% endfor %}
{% if goods.has_next %}
<a href="/show?page={{ goods.next_num }}">下一页</a>
{% endif %}
1
2
3
4
5
6
@app.route('/show/')
def show():
page = int(request.args.get('page',1))
goods = GOOds.query.paginate(page,per_page=3)
# app_list = app_sql.session.query(app_sql.Subordinate_forces).offset(num * 10).limit(10).all()
return render_template('show.html',goods=goods)

下载判断

@page.route(“/goodsdown”)
def download():
goodslist = Goods.query.all()
data = []
for goods in goodslist:
temp = {}
temp[“id”] = goods.id
temp[“name”] = goods.name
temp[“price”] = goods.price
temp[“img”] = goods.img

    data.append(temp)


# 每次下载 向数据库中写入count
count = Count.query.first()


print("count", count)
if not count:
    count = Count(count=1)
    db.session.add(count)
    db.session.commit()
else:
    count.count += 1
    db.session.commit()


with open("static/data.json", "w") as f:
    jstr = json.dumps(data, ensure_ascii=False)
    f.write(jstr)


return send_file("static/data.json", as_attachment=True, cache_timeout=1)
作者

Kawakami Ari

发布于

2022-11-30

更新于

2022-11-29

许可协议