Flask 分页验证码心得

from flask import *
from flask_sqlalchemy import SQLAlchemy
from utils.captcha.captcha import captcha

# 上传图片的配置
from flask_uploads import UploadSet, IMAGES, configure_uploads
import sys
import os


class Config(object):
"""工程配置信息"""
DEBUG = True
"""SQLAlchemy 配置"""
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://[email protected]:3306/ceshi'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_COMMIT_ON_TEARDOWN = True # 数据库内容发生改变之后,自动提交 (不会及时提交,需要马上刷新的时候自己commit)
# 查询时会显示原始SQL语句
# SQLALCHEMY_ECHO = True
SECRET_KEY = '1810'
# 日志的级别
LEVEL = 'DEBUG'


app = Flask(__name__)
db = SQLAlchemy(app)
app.config.from_object(Config)

# 图片上传配置
fn = getattr(sys.modules['__main__'], '__file__')
# 保存图片的目录
root_path = os.path.abspath(os.path.dirname(fn)) + "/static/upload"
app.config['UPLOADED_PHOTO_DEST'] = root_path
app.config['UPLOADED_PHOTO_ALLOW'] = IMAGES
photos = UploadSet('PHOTO') # 操作图片的实例
configure_uploads(app, photos)


# 生成验证码图片
@app.route('/get_image')
def get_image():
# name, text, StringIO.value
# text : 验证码图片对应到到文本
# image_url : 验证码图片IO流。理解为:二进制数据,并没有实际转换成图片呢
name, text, image_url = captcha.generate_captcha()
session['img_code'] = text
response = make_response(image_url) # 生成图片到响应
# 告诉浏览器,我要返回到是一张图片
response.headers['Content-type'] = 'image/jpg'
return response


class Category(db.Model):
__tablename__ = 'category'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200))
pro = db.relationship('Products', backref='category')


class Products(db.Model):
__tablename__ = 'products'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200))
c_id = db.Column(db.Integer, db.ForeignKey('category.id'))


@app.route('/init')
def init():
db.create_all()
return '创建成功!'


# 添加分类
@app.route('/add_cate', methods=['GET', 'POST'])
def add_cate():
if request.method == 'POST':
cate_name = request.form.get('cate_name')
c = Category(name=cate_name)
db.session.add(c)
db.session.commit()
flash('添加成功!')
return render_template('add_cate.html')


# 添加产品
@app.route('/add_pro', methods=['GET', 'POST'])
def add_pro():
d = {}
c = Category.query.all()
d['cates'] = c
if request.method == 'POST':
cate_id = request.form.get('cate_id')
pro_name = request.form.get('pro_name')
p = Products(name=pro_name, c_id=cate_id)
db.session.add(p)
db.session.commit()
flash('添加成功!')
return render_template('add_pro.html', data=d)


# 首页展示
@app.route('/', methods=['GET', 'POST'])
def index():
d = {}
c = Products.query.all()
d['info'] = c

page = request.args.get('page', 1)
products_list = Products.query.order_by(Products.id.desc()).paginate(int(page), 3, False)

# 数据
d = {}
d['info'] = products_list.items # 经过筛选的产品列表
d['current_page'] = products_list.page # 当前页
d['total_page'] = products_list.pages # 总页数

if request.method == 'POST':
a = request.form.getlist('a')
for z in a:
q = Products.query.filter(Products.id == z).first()
db.session.delete(q)
db.session.commit()
return redirect(url_for('index'))
return render_template('index.html', data=d)


# 分类下的产品详情
@app.route('/detail/<int:id>')
def detail(id):
d = {}
p = Products.query.filter(Products.c_id == id).order_by(Products.id.desc()).all()
d['info'] = p
return render_template('detail.html', data=d)


if __name__ == '__main__':
app.run()

猜你喜欢

转载自www.cnblogs.com/lhrd/p/10764587.html