Flask-分类和商品(知识点:分页,批量删除)

    xiaoxiao2022-07-14  186

    题目要求:

    目录结构:

    其中static目录下为分页插件

    一、py文件:

    # start.py from apps import * from models import * from moduls.web.index import index_blue app.register_blueprint(index_blue) @app.before_request def g_cates_data(): cates = Fenlei.query.all() g.cates = cates if __name__ == '__main__': # db.drop_all() # db.create_all() app.run() # setting.py DEBUG = True SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:mysql@127.0.0.1:3306/test?charset=utf8' SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = '123' SQLALCHEMY_COMMIT_ON_TEARDOWN = True # apps.py from flask import Flask,render_template,request,url_for,redirect,flash,Blueprint,g from flask_sqlalchemy import SQLAlchemy app=Flask(__name__) app.config.from_object('setting') db=SQLAlchemy(app=app) # models.py from apps import * class Fenlei(db.Model): id = db.Column(db.Integer,primary_key=True) name=db.Column(db.String(30)) class Goods(db.Model): id = db.Column(db.Integer,primary_key=True) sname=db.Column(db.String(30)) fid=db.Column(db.Integer,db.ForeignKey(Fenlei.id)) # manager.py from models import * from flask_script import Manager from flask_migrate import Migrate,MigrateCommand manager=Manager(app=app) migrate=Migrate(app=app,db=db) manager.add_command('db',MigrateCommand) if __name__ == '__main__': manager.run()

    二、modules/web 目录:

    # index.py from models import * index_blue=Blueprint('web',__name__) @index_blue.route('/') def index(): return render_template('index.html') @index_blue.route('/add_fenlei') def add_fenlei(): one_name1=Fenlei(name='分类1') one_name2=Fenlei(name='分类2') one_name3=Fenlei(name='分类3') db.session.add_all([one_name1,one_name2,one_name3]) return '添加分类成功' @index_blue.route('/all_fenlei') def all_fenlei(): all_fenlei=Fenlei.query.order_by(Fenlei.id.desc()).all() return render_template('all_fenlei.html',all_fenlei=all_fenlei) @index_blue.route('/add_goods',methods=['GET','POST']) def add_goods(): fid=request.args.get('fid') if request.method=='POST': sname=request.form.get('sname') one_goods=Goods(sname=sname,fid=fid) db.session.add(one_goods) return render_template('add_goods.html') @index_blue.route('/all_goods') def all_goods(): fid=request.args.get('fid') one_goods=Goods.query.filter(Goods.fid==fid).order_by(Goods.id.desc()).all() return render_template('all_goods.html',one_goods=one_goods) @index_blue.route('/all_good') def all_good(): page = int(request.args.get('page', 1)) one_goods=Goods.query.order_by(Goods.id.desc()).paginate(page,5) return render_template('all_good.html',one_goods=one_goods) @index_blue.route('/delete_goods',methods=['GET','POST']) def delete_goods(): fid=request.form.getlist('fid') # one_goods=Goods.query.get(fid) # db.session.delete(one_goods) # return redirect(url_for('web.all_goods')) for i in fid: one_goods=Goods.query.get(int(i)) db.session.delete(one_goods) return redirect(url_for('web.all_fenlei'))

    三、templates 目录:

    {# base.html #} {% block top %} <a href="{{ url_for('web.index') }}">首页</a> <a href="{{ url_for('web.all_fenlei') }}">所有分类</a> <a href="{{ url_for('web.all_good') }}">所有商品</a> <br> {% endblock top %} {% block content %} {% endblock content %} {# index.html #} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% extends 'base.html' %} {% block content %} <h1>首页</h1> {% endblock content %} </body> </html> {# add_goods.html #} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% extends 'base.html' %} {% block content %} <form action="" method="post"> 商品名称:<input type="text" name="sname" value=""> <input type="submit" value="添加商品"> </form> {% endblock content %} </body> </html> {# all_fenlei.html #} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body> {% extends 'base.html' %} {% block content %} {% for i in all_fenlei %} {{ i.id }}{{ i.name }} <a href="{{ url_for('web.add_goods',fid=i.id) }}">添加商品</a> <a href="{{ url_for('web.all_goods',fid=i.id) }}">商品</a> <br> {% endfor %} {% endblock content %} </body> </html> {# all_good.html #} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> {# <script src="../static/jquery.pagination.min.js"></script>#} </head> <body> {% extends 'base.html' %} {% block content %} {# <form action="" >#} {# {% for foo in one_goods.items %}#} {# {{ foo.id }}{{ foo.sname }}|#} {# {% for i in g.cates %}#} {# {% if i.id == foo.fid %}#} {# {{ i.name }}#} {# {% endif %}#} {# {% endfor %}#} {# <br>#} {# {% endfor %}#} {# </form>#} <form action="{{ url_for('web.delete_goods') }}" method="post"> {% for foo in one_goods.items %} <input type="checkbox" name="fid" value="{{ foo.id }}"> {{ foo.id }}{{ foo.sname }}| {% for i in g.cates %} {% if i.id == foo.fid %} {{ i.name }} {% endif %} {% endfor %} <br> {% endfor %} <input type="submit" value="批量删除"> </form> <a href="{{ url_for('web.all_good',page=1) }}">1</a> <a href="{{ url_for('web.all_good',page=2) }}">2</a> {% endblock %} </body> </html> {# all_goods.html #} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% extends 'base.html' %} {% block content %} <form action="{{ url_for('web.delete_goods') }}" method="post"> {% for foo in one_goods %} <input type="checkbox" name="fid" value="{{ foo.id }}"> {{ foo.id }}{{ foo.sname }}| {% for i in g.cates %} {% if i.id == foo.fid %} {{ i.name }} {% endif %} {% endfor %} <br> {% endfor %} <input type="submit" value="批量删除"> </form> {% endblock content %} </body> </html>
    最新回复(0)