简体   繁体   English

如何将查询变量从一个HTML页面传送到另一个HTML页面?

[英]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.

相关问题 存储在变量中的值不会从一个 function 转移到另一个 - Values stored in variables dont carry over from one function to another Django:如何将模型表单数据从一个页面传送到另一个页面,然后返回,而无需提交给DB? - Django: How to carry model form data from one page to another, and back, without commiting to the DB? 如何将变量从一个类转移到另一个类? - How do I carry over a variable from one class to another? 我如何将数据(变量)从一个 html 页面传输到另一个页面,这里我使用 html 和 django 框架 - How i transfer data(variables) from one html page to another page, here i am using html & django framework Python 和 Tkinter,将列表信息从一个函数传送到另一个函数 - Python and Tkinter, carry list info from one function to another Python:函数-将结果从一个函数传递到另一个函数 - Python: Functions - carry the results from one function into another 如何将 javascript 变量值从一个 html 页面传递到另一个 html 页面? - How to pass the javascript variable value from one html page to the another html page? 如何将变量从导入的函数携带到主文件(Python 3.8.3 - How to carry variables from imported functions to the main file (Python 3.8.3 如何从一种方法访问变量到另一种方法? - How to access variables from one method into another? 您如何从另一个定义继承值? - How do you carry over values from another def?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM