用Flask實現(xiàn)簡單搜索功能主要是通過form的方式傳值,,再到數(shù)據(jù)庫中查詢。下面是數(shù)據(jù)庫的內(nèi)容,,主要是實現(xiàn)對content進行模糊匹配,。 圖 1 數(shù)據(jù)庫內(nèi)容 1.首先打開pycharm,構(gòu)建一個最簡單的flask應(yīng)用,。 #search.py from flask import Flask app = Flask(__name__) @app.route("/") def index(): return render_template('search.html') if __name__ =="__main__": app.run() |
2.其次,,連接數(shù)據(jù)庫進行搜索,要引入SQLAlchemy包,。 代碼示例: #search.py from flask_sqlalchemy import SQLAlchemy app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:[email protected]:3306/test?charset=utf8' app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True #root用戶名123456密碼 test數(shù)據(jù)庫 |
3.最后,,需要從form表單拿到數(shù)據(jù),進行數(shù)據(jù)庫查詢操作,。 代碼示例: @app.route('/', methods=['post', 'get']) def search(): content = request.form.get('content') #需要查詢的內(nèi)容 if content is None: content = " " quotes = Quotes.query.filter(Quotes.content.like("%"+content+"%")if content is not None else "").all() #查詢跟content有關(guān)的數(shù)據(jù),,返回結(jié)果為列表 return render_template('search.html',quotes = quotes) #將查詢結(jié)果返回到前端 | #search,html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post" action="{{url_for('search')}}"> <input type="text" name="content" id="content" placeholder="請輸入搜索內(nèi)容"> <input type="submit" value="搜索"> </form> <div> <table border="1px" cellspacing="0px"> {% for i in quotes %} <tr> <td>名字</td> <td>{{i.name}}</td> <td>內(nèi)容</td> <td>{{i.content}}</td> </tr> {% endfor %} </table> </div> </body> </html> |
效果如下: 圖 2、3 效果圖
一個簡單的查詢功能就完成了,,這里只是做了單條件模糊查詢,。接下來介紹多條件模糊查詢和精確查詢。 代碼示例: #多條件模糊查詢 content = Quotes.query.filter( Quotes.id.like("%" + id + "%") if id is not None else "", Quotes.name.like("%" + name + "%") if name is not None else "", Quotes.content.like("%" + content + "%") if content is not None else "" ).all() |
#單條件精確查詢 content = Quotes.query.filter_by(id=id).all() | #多條件精確查詢 content = Quotes.query.filter_by(id=id,name=name).all() |
最后附上完整代碼: #search.py from flask import Flask, config, render_template, request, flash, url_for, session from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_object(config) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:[email protected]:3306/java2019?charset=utf8' app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True db = SQLAlchemy(app) class Quotes(db.Model): id = db.Column(db.Integer, primary_key=True, comment='ID') name = db.Column(db.String(255), unique=True, comment='名稱') content = db.Column(db.String(255), comment='內(nèi)容') @app.route('/', methods=['post', 'get']) def search(): content = request.form.get('content') if content is None: content = " " quotes = Quotes.query.filter(Quotes.content.like("%"+content+"%")if content is not None else "").all() return render_template('search.html',quotes = quotes) if __name__ == "__main__": app.run(debug=True) |
#search.py <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post" action="{{url_for('search')}}"> <input type="text" name="content" id="content" placeholder="請輸入搜索內(nèi)容"> <input type="submit" value="搜索"> </form> <div> <table border="1px" cellspacing="0px"> {% for i in quotes %} <tr> <td>名字</td> <td>{{i.name}}</td> <td>內(nèi)容</td> <td>{{i.content}}</td> </tr> {% endfor %} </table> </div> </body> </html> |
編 輯 | 王文星 責 編 | 劉玉江
|