[英]How to carry query variables from one html page to another?
I've been attempting to pass the value q to the second @app.route for some time now but have had no success. 我已经尝试将值q传递给第二个@ app.route一段时间了,但是没有成功。
@app.route("/results")
def results():
q = request.args.get('q')
r = requests.get('http://api.yummly.com/v1/api/recipes?_app_id=MYAPPID&_app_key=MYAPPKEY&requirePictures=true&q= %s' % q)
data = r.json()
return render_template("results.html", recipename = data['matches'][0]['recipeName'])
@app.route("/recipe")
def recipe():
r = requests.get('http://api.yummly.com/v1/api/recipes?_app_id=MYAPPID&_app_key=MYAPPKEY&requirePictures=true&q= %s' % q)
data = r.json()
ID = data["matches"][0]['id']
rr = requests.get('http://api.yummly.com/v1/api/recipe/%s?_app_id=dd85df7a&_app_key=0b187b4352ca796bc89a4600dfad0f06' % ID )
rdata = rr.json()
return render_template("recipe.html", url = rdata['attribution']['url'])
Any help on how to pass this value would be much appreciated. 任何有关如何传递此值的帮助将不胜感激。
The first route does not call the second one, so it's misleading to say that you want to "pass" the data. 第一条路线没有调用第二条路线,因此说您要“传递”数据是一种误导。
It sounds like you want the first route to save the data somewhere, and the second route to read the data. 听起来您想要第一个路由将数据保存在某个地方,第二个路由读取数据。 There are several ways to do this, such as saving the data in a plain file or in a database.
有几种方法可以做到这一点,例如将数据保存在纯文件或数据库中。 However then you have to worry about one user's data being visible to other users, so it might be best to save the data within the session.
但是,随后您必须担心一个用户的数据对其他用户可见,因此最好将数据保存在会话中。
You have another problem: since there is no direct linkage between the two routes, how do you ensure synchronization? 您还有另一个问题:由于两条路由之间没有直接链接,因此如何确保同步? What if the user visits the second route without visiting the first one?
如果用户访问第二条路线而不访问第一条路线怎么办?
One solution would be to modify your second route so it requires the data to be passed in as an argument, instead of depending on some previous route that may or may not have been visited recently (or ever!) 一种解决方案是修改第二条路线,以便它需要将数据作为参数传递,而不是依赖于最近(或曾经!)访问过或未曾访问过的某些以前的路线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.