from flask import Flask,render_template,request,redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import and_
import pymysql
app = Flask(__name__) # 实例化Flask对象
# 基本配置
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = (
'mysql+pymysql://root:root@localhost/flask_demo'
)
db = SQLAlchemy(app) # 实例化SQLAlchemy
class Hotel(db.Model):
''' Hotel表模型'''
__tablename__ = 'hotel' # 表名
id = db.Column(db.Integer,primary_key=True) # 主键ID
name = db.Column(db.String(64)) # 名称
level = db.Column(db.Integer) # 等级
brand = db.Column(db.String(64)) # 品牌
features = db.Column(db.String(64)) # 特色
score = db.Column(db.Float) # 评分
price = db.Column(db.Float) # 最低价
decorate_time = db.Column(db.Date) # 装修日期
@app.route('/hotel')
def index():
# 获取所有品牌并去重
brand_list = db.session.query(Hotel.brand).distinct()
# 获取所有酒店特色并去重
features_list = db.session.query(Hotel.features).distinct()
condition = (Hotel.id > 0)
# 根据条件筛选酒店信息
if request.args.get('level'): # 等级
level = request.args.get('level')
condition = and_(condition,Hotel.level==level)
if request.args.get("brand"): # 品牌
brand = request.args.get('brand')
condition = and_(condition, Hotel.brand==brand)
if request.args.get("features"): # 特色
features = request.args.get("features")
condition = and_(condition, Hotel.features==features)
# 排序方式
if request.args.get("tag"):
order_string = request.args.get("tag") + ' desc'
else :
order_string = 'id desc'
hotel = Hotel.query.filter(condition).order_by(order_string) # 执行查询
return render_template('hotel.html',hotel=hotel,brand_list=brand_list,features_list=features_list)
if __name__ == "__main__":
app.run(debug=True) # 启动项目
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Python 使用Flask-SQLAlchemy实现数据的多对关系 Python源码Python 使用Flask-SQLAlchemy实现数据的多对关系 Python源码Python 使用Flask-SQLAlchemy实现数据的多对关系 Python源码Python 使用Flask-SQLAlchemy实现数据的多对关系 Python源码Python 使用Flask-SQLAlchemy实现数据的多对关系 Python源码Python 使用Flask-SQLAlchemy实现数据的多对关系 Python源码Python 使用Flask-SQLAlchemy实现数据的多对关系 Python源码Python 使用Flask-SQLAlchemy实现数据的多对关系 Python源码
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 6 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/262c377314c848f7b63c5c6d964eaba7_douluo998.jpg!1)
douluo998
- 粉丝: 2119
- 资源: 5356
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)