flask知识点:逻辑:indexfrom flask import *from flask_sqlalchemy import SQLAlchemy# 导入captcha 用于生成验证码from captcha.captcha import captchafrom flask_uploads import UploadSet, IMAGES, configure_uploadsimport sysimport osimport re app = Flask(__name__)app.secret_key = ‘1812B1234567890‘app.config[‘SQLALCHEMY_DATABASE_URI‘] = ‘mysql+pymysql://root:[email protected]:3306/KaoShi‘db = SQLAlchemy(app) fn = getattr(sys.modules[‘__main__‘], ‘__file__‘)root_path = os.path.abspath(os.path.dirname(fn)) + "/static/upload"app.config[‘UPLOADED_PHOTO_DEST‘] = root_pathapp.config[‘UPLOADED_PHOTO_ALLOW‘] = IMAGESphotos = UploadSet(‘PHOTO‘) # 用来保存图片的对象 photos.save()configure_uploads(app, photos) # 用户表class User(db.Model): __tablename__ = ‘user‘ id = db.Column(db.Integer, primary_key=True) user_name = db.Column(db.String(200)) pwd = db.Column(db.String(200)) tel = db.Column(db.String(20)) # 分类表class Cate(db.Model): __tablename__ = ‘cate‘ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) goods_list = db.relationship(‘Goods‘, backref=‘cate‘) # 商品表class Goods(db.Model): __tablename__ = ‘goods‘ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) img_url = db.Column(db.String(500)) price = db.Column(db.DECIMAL(10,2), default=9999999.99) # 详情,需要使用图文混排输入 content = db.Column(db.Text) c_id = db.Column(db.Integer, db.ForeignKey(‘cate.id‘)) # 首页@app.route(‘/‘)def index(): d={} # 加载数据,展示页面 cate_list = Cate.query.all() d[‘cate_list‘] = cate_list return render_template(‘首页.html‘, data=d) # 列表页@app.route(‘/view/<c_id>‘, methods=[‘GET‘, ‘POST‘])def view(c_id): """根据分类ID查询对应到商品列表,并展示""" if request.method == ‘POST‘: # 开始批量删除 # 获取数据,被选择的商品ID列表 del_list = request.form.getlist(‘select‘) for g_id in del_list: g = Goods.query.get(g_id) db.session.delete(g) db.session.commit() current_page = request.args.get(‘page‘, 1, type=int) # 当前页 per_page = 2 # 每页显示的条数 d = {} # 加载数据,展示页面 # 菜单 cate_list = Cate.query.all() d[‘cate_list‘] = cate_list # 商品列表 goods_list = Goods.query.filter(Goods.c_id == c_id).paginate(current_page, 2) d[‘goods_list‘] = goods_list.items # 商品列表信息 d[‘current_page‘] = goods_list.page # 当前页 d[‘total_page‘] = goods_list.pages # 总页数 d[‘c_id‘] = c_id # 当前分类ID # 展示页面 return render_template(‘列表页.html‘, data=d) # 商品详情@app.route(‘/detail/<g_id>‘)def detail(g_id): d = {} goods = Goods.query.get(g_id) d[‘goods‘] = goods return render_template(‘商品详情页.html‘, data=d) # 注册@app.route(‘/register‘, methods=[‘GET‘, ‘POST‘])def register(): if request.method == ‘POST‘: # 1、获取数据 # 用户名 user_name = request.form.get(‘user_name‘) # 密码 pwd = request.form.get(‘pwd‘) # 手机号 tel = request.form.get(‘tel‘) # 验证码 img_code = request.form.get(‘img_code‘) # 2、验证数据(验证码、正则匹配手机号) if all([user_name, pwd, tel, img_code]): # 正则验证手机号 if re.match(‘1[3-9]\d{9}‘, tel): # 验证码 - 忽略大小写 lower() if img_code.lower() == session.get(‘img_code‘).lower(): # 3、组织数据,(生成一个 User类 的实例) u = User() u.user_name = user_name u.pwd = pwd u.tel = tel # 4、数据写入到数据库 db.session.add(u) db.session.commit() return redirect(url_for(‘index‘)) else: flash(‘验证码不正确‘) else: flash(‘手机号格式错误‘) else: flash(‘数据不完整‘) # 展示注册页面 return render_template(‘注册页面.html‘) # 生成验证码图片@app.route(‘/get_image‘)def get_image(): name, text, image = captcha.generate_captcha() # 保存验证码图片 内容(文本) session[‘img_code‘] = text # 返回对象Response res = make_response(image) res.headers[‘Content-Type‘] = ‘image/jpg‘ return res # 添加分类@app.route(‘/add_cate‘, methods=[‘GET‘, ‘POST‘])def add_cate(): if request.method == ‘POST‘: # 获取数据 cate_name = request.form.get(‘name‘) # 验证数据 if all([cate_name]): # 判断分类是否存在 c_temp = Cate.query.filter(Cate.name == cate_name).first() if c_temp: flash(‘类别已经存在‘) else: # 添加到数据库 c = Cate(name=cate_name) db.session.add(c) db.session.commit() flash(‘添加成功‘) else: flash(‘信息不完整‘) # 展示页面 d={} cate_list = Cate.query.all() d[‘cate_list‘] = cate_list return render_template(‘后台添加分类.html‘, data=d) # 添加商品@app.route(‘/add_goods‘, methods=[‘GET‘, ‘POST‘])def add_goods(): # 添加商品 if request.method == "POST": # 1、获取数据 cate_id = request.form.get(‘cate‘) # 分类ID name = request.form.get(‘name‘) price = request.form.get(‘price‘) img = request.files.get(‘img‘) content = request.form.get(‘content‘) # 2、验证数据 if all([cate_id, name, price, content]): # 3、整理数据。上传图片。生成一个Goods的实例 g = Goods() g.name = name g.price = price g.content = content g.c_id = cate_id # 判断用户是否选择了图片 if img: filename = photos.save(img) img_path = ‘/static/upload/‘ + filename g.img_url = img_path # 4、写入到数据库 db.session.add(g) db.session.commit() flash(‘添加商品成功‘) else: flash(‘信息不完整‘) # 展示页面 d={} cate_list = Cate.query.all() d[‘cate_list‘] = cate_list return render_template(‘后台添加商品.html‘, data=d) # 保存富文本的图片@app.route(‘/submit-image‘, methods=[‘GET‘, ‘POST‘])def submit_image(): # 获取文件 get(‘file‘) 为固定写法 img = request.files.get(‘file‘) image_name = photos.save(img) # 将头像保存到 程序目录下 image_url = ‘/static/upload/‘ + image_name # 生成图片保存到地址 mes = {} mes[‘path‘] = image_url # key为固定值,不可随意修改 mes[‘error‘] = False # key为固定值,不可随意修改 return jsonify(mes) if __name__ == ‘__main__‘: # db.drop_all() # db.create_all() app.run()
原文地址:https://www.cnblogs.com/Jacky-WYQ/p/10915733.html
时间: 2024-10-19 16:15:37