Flask 将表单参数传递给函数
我是Flask的新手,我只是想把从表单中得到的4个参数传递给另一个python函数。当我提交表单时,我遇到了以下错误:“TypeError:search()缺少4个必需的位置参数:'关键字','开始日期','结束日期',和'国家'” 我验证了,我所有的变量都有表单中的数据,所以它们不是空的 `Flask 将表单参数传递给函数,flask,Flask,我是Flask的新手,我只是想把从表单中得到的4个参数传递给另一个python函数。当我提交表单时,我遇到了以下错误:“TypeError:search()缺少4个必需的位置参数:'关键字','开始日期','结束日期',和'国家'” 我验证了,我所有的变量都有表单中的数据,所以它们不是空的 ` 您需要使用表单参数呈现模板 试试这个: @app.route('/index', methods=["GET", "POST"]) def index():
您需要使用表单参数呈现模板 试试这个:
@app.route('/index', methods=["GET", "POST"])
def index():
if request.method == "POST":
keywords = request.form.get('keywords')
starting_date = request.form.get('starting_date')
ending_date = request.form.get('ending_date')
country = request.form.get('country')
return render_template('result.html', keywords=keywords, starting_date=starting_date, ending_date=ending_date, country=country)
else:
return render_template("index.html")
您需要使用表单参数呈现模板 试试这个:
@app.route('/index', methods=["GET", "POST"])
def index():
if request.method == "POST":
keywords = request.form.get('keywords')
starting_date = request.form.get('starting_date')
ending_date = request.form.get('ending_date')
country = request.form.get('country')
return render_template('result.html', keywords=keywords, starting_date=starting_date, ending_date=ending_date, country=country)
else:
return render_template("index.html")
您需要将
搜索路线定义为
@app.route('/search/<keywords>/<starting_date>/<ending_date>/<country>', methods=["GET"])
现在
的url\u将生成/index?关键字=&start\u日期=…
查看此伟大的SO答案,了解从请求获取数据的所有方法:您需要将您的搜索路径定义为
@app.route('/search/<keywords>/<starting_date>/<ending_date>/<country>', methods=["GET"])
现在
的url\u将生成/index?关键字=&start\u日期=…
查看此伟大的SO答案,了解从请求获取数据的所有方法:或者您可以简单地执行keywords=request.args['keywords']
:)或者您可以简单地执行keywords=request.args['keywords']
:)