我是Flask的新手,想知道是否可以使用相同的URL在html中显示表单并查询以显示某些东西
理想情况下,我希望发生以下结果。
如果要通过执行以下操作在138.10.2.1/sample中创建查询:
http://138.10.2.1:8000/sample?psi=1&lavr=1&dsc=1&ifsc=1&ics=1&eng=3&ol1=1&ol2=1&reso=1&educ=1&listen=1&time=1&probe=1&unders=1
它将显示:
*something in json format*
在网页中
否则,如果我直接讲:
http://138.10.2.1:8000/sample
它将引导我进入一个.html页面,其中包含一个表单以填充或允许用户附加文件以使用和上传,并以json格式显示转换后的文件。
这是我的代码
sample.py
from flask import Flask, flash, request, redirect, url_for, make_response, send_from_directory, render_template
import convert as ps
app = Flask(__name__)
@app.route("/sample", methods=["GET", "POST"])
def query_strings():
#This is not working: if request.method == "POST":
args1 = request.args["psi"]
args2 = request.args["lavr"]
args3 = request.args["dsc"]
args4 = request.args["ifsc"]
args5 = request.args["ics"]
args6 = request.args["eng"]
args7 = request.args["ol1"]
args8 = request.args["ol2"]
args9 = request.args["reso"]
args10 = request.args["educ"]
args11 = request.args["listen"]
args12 = request.args["time"]
args13 = request.args["probe"]
args14 = request.args["unders"]
args_list = [args1, args2, args3, args4, args5, args6, args7, args8,args9, args10, args11, args12, args13, args14]
result = ps.execute(args_list)
response = app.response_class(
response=result,
status=200,
mimetype='application/json'
)
return response
#This is my html form: return render_template("form.html")
if __name__ == '__main__':
app.run(debug = True)
现在,我所能做的就是运行查询,但是如果我刚刚输入,我将被提示输入我声明的参数:
http://138.10.2.1:8000/sample
答案 0 :(得分:1)
您可以检查参数的数量,如果长度为0,则返回HTML表单,如下所示:
# if there are no arguments provided, show HTML form
if len(request.args) == 0:
return render_template("form.html")
此外,也不需要将每个参数存储为单独的变量,然后将它们组合成一个列表。 request.args
已经是一个字典,因此您只需使用以下命令即可获取参数名称和值的列表:
list(request.args.keys()) # list of argument names eg. ['psi', 'lavr', 'dsc', 'ifsc'...]
list(request.args.values()) # list of argument values eg. [1, 1, 1, 1...]
您可以检查参数名称是否与所需集合匹配:
if set(argument_names) == {"psi","lavr","dsc","ifsc","ics","eng","ol1","ol2","reso","educ","listen","time","probe","unders"}:
# return JSON
总体而言,您的代码可能如下所示:
from flask import Flask, request, render_template
import json
app = Flask(__name__)
@app.route("/sample", methods=["GET", "POST"])
def query_strings():
# if there are no arguments provided, show HTML form
if len(request.args) == 0:
return render_template("form.html")
argument_names = list(request.args.keys())
# if the argument list is valid
if set(argument_names) == {"psi","lavr","dsc","ifsc","ics","eng","ol1","ol2","reso","educ","listen","time","probe","unders"}:
# return JSON
response = app.response_class(
response=json.dumps(request.args),
status=200,
mimetype='application/json'
)
return response
return "Invalid arguments"
if __name__ == '__main__':
app.run(debug = True)
这将:
form.html
,请显示/sample
/sample
)加载/sample?psi=1&lavr=1&dsc=1&ifsc=1&ics=1&eng=3&ol1=1&ol2=1&reso=1&educ=1&listen=1&time=1&probe=1&unders=1
,则将参数显示为JSON 答案 1 :(得分:1)
哦..我明白了..so如果不输入任何参数怎么显示.html?这将允许我在html表单中附加文件并创建到POST并获得相同的json结果
根据您对问题的评论和您发布的示例代码,我认为您可能正在寻找这样的东西:
@app.route("/sample", methods=["GET", "POST"])
def query_strings():
args1 = request.args.get("psi")
args2 = request.args.get("lavr")
args3 = request.args.get("dsc")
args4 = request.args.get("ifsc")
args5 = request.args.get("ics")
args6 = request.args.get("eng")
args7 = request.args.get("ol1")
args8 = request.args.get("ol2")
args9 = request.args.get("reso")
args10 = request.args.get("educ")
args11 = request.args.get("listen")
args12 = request.args.get("time")
args13 = request.args.get("probe")
args14 = request.args.get("unders")
args_list = [
args1, args2, args3, args4, args5, args6, args7, args8,
args9, args10, args11, args12, args13, args14
]
if not all(args_list):
return render_template('form.html')
else:
result = ps.execute(args_list)
response = app.response_class(
response=result,
status=200,
mimetype='application/json'
)
return response
在这种情况下,如果您在GET请求中未提供任何参数,它将以html格式呈现模板。
也不要总是检查请求方法,我建议您看一下Flask的MethodView。使用它,您可以很好地将逻辑拆分为带有查询字符串中的参数的请求和使用json提交的表单: http://flask.pocoo.org/docs/1.0/api/#flask.views.MethodView
答案 2 :(得分:0)
尝试此代码,希望对您有所帮助。默认情况下,它使用GET方法,因此它不起作用。当您单击提交按钮时,它将调用POST方法。
from flask import Flask, flash, request, redirect, url_for, make_response, send_from_directory, render_template
import convert as ps
app = Flask(__name__)
@app.route("/sample", methods=["GET", "POST"])
def query_strings():
if request.method == "POST":
args1 = request.args["psi"]
args2 = request.args["lavr"]
args3 = request.args["dsc"]
args4 = request.args["ifsc"]
args5 = request.args["ics"]
args6 = request.args["eng"]
args7 = request.args["ol1"]
args8 = request.args["ol2"]
args9 = request.args["reso"]
args10 = request.args["educ"]
args11 = request.args["listen"]
args12 = request.args["time"]
args13 = request.args["probe"]
args14 = request.args["unders"]
args_list = [args1, args2, args3, args4, args5, args6, args7, args8,args9, args10, args11, args12, args13, args14]
result = ps.execute(args_list)
response = app.response_class(
response=result,
status=200,
mimetype='application/json'
)
# return response
return render_template("form.html", response = response)
return render_template("form.html")
#This is my html form: return render_template("form.html")
if __name__ == '__main__':
app.run(debug = True)