import config
from flask import Flask, jsonify, request
## app and db is defined on model.py
from model import db, app, PLAY_STORE
@app.route('/app-api', methods=['GET'])
def listapp():
if request.method == 'GET':
store=request.args.get('store')
category=request.args.get('category')
results = PLAY_STORE.query.filter_by(Store=store,Category=category).all()
json_results = []
for result in results:
d = {'rank': result.Rank,
'store': result.Store,
'category': result.Category,
'type': result.Type,
'title': result.Title}
json_results.append(d)
return jsonify(items=json_results)
if __name__ == '__main__':
app.run(debug=True)
I am trying to build a rest api on top of mysql database using flask. In the above program I am trying to get the store and category from the url. Even though I use request.args.get
, Store value is empty,i.e json_results is empty.If I hard code making PLAY_STORE.query.filter_by(Store="Play Store")
i am getting the expected output.But when i type 127.0.0.1/app-api?store="Play Store"
json_results is empty.
我正在尝试使用flask在mysql数据库之上构建一个rest api。在上面的程序中,我试图从网址获取商店和类别。即使我使用request.args.get,Store值为空,即json_results为空。如果我硬编码PLAY_STORE.query.filter_by(Store =“Play Store”)我得到了预期的输出。但是当我输入127.0时.0.1 / app-api?store =“Play商店”json_results为空。
I have one more issue .How to put multiple conditions in the filter_by, something like PLAY_STORE.query.filter_by(Store="Play Store" and Category="Games")
.
我还有一个问题。如何在filter_by中添加多个条件,例如PLAY_STORE.query.filter_by(Store =“Play Store”和Category =“Games”)。
1 个解决方案
#1
0
Problem is with your GET url parameters
问题在于你的GET url参数
Try this url:
试试这个网址:
127.0.0.1:5000/app-api?store=Play Store&category=Games
You don't need to send single quotes in querystring values i.e
您不需要在查询字符串值中发送单引号,即
<your-host>/?store=XXXXXX not like this <your-host>/?store='XXXXXX'
Also, filter_by
is used for simple queries, If you want to make complex Queries use filter and for more info go through this * question
此外,filter_by用于简单查询,如果您想使复杂查询使用过滤器,有关更多信息,请查看此*问题
#1
0
Problem is with your GET url parameters
问题在于你的GET url参数
Try this url:
试试这个网址:
127.0.0.1:5000/app-api?store=Play Store&category=Games
You don't need to send single quotes in querystring values i.e
您不需要在查询字符串值中发送单引号,即
<your-host>/?store=XXXXXX not like this <your-host>/?store='XXXXXX'
Also, filter_by
is used for simple queries, If you want to make complex Queries use filter and for more info go through this * question
此外,filter_by用于简单查询,如果您想使复杂查询使用过滤器,有关更多信息,请查看此*问题