Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在flask中的路由之间传递参数_Python_Python 3.x_Flask_Web Applications_Global Variables - Fatal编程技术网

Python 在flask中的路由之间传递参数

Python 在flask中的路由之间传递参数,python,python-3.x,flask,web-applications,global-variables,Python,Python 3.x,Flask,Web Applications,Global Variables,我正在创建一个基于Flask的web应用程序。在我的主页上,我从用户那里获取某些输入,并在其他路径中使用它们来执行某些操作。我目前正在使用global,但我知道这不是一个好方法。 我在Flask中查找了会话,但我的web应用程序没有注册用户,所以我不知道在这种情况下会话将如何工作。简言之: Webapp不要求用户注册 用户选择通过表单传递三个参数列表 这三个列表,浮点数列表、字符串列表和整数列表,必须传递给其他路由以处理信息 有什么好办法吗 您可以通过url参数从主页传递用户输入。i、 e您

我正在创建一个基于Flask的web应用程序。在我的主页上,我从用户那里获取某些输入,并在其他路径中使用它们来执行某些操作。我目前正在使用
global
,但我知道这不是一个好方法。 我在Flask中查找了
会话
,但我的web应用程序没有注册用户,所以我不知道在这种情况下会话将如何工作。简言之:

  • Webapp不要求用户注册
  • 用户选择通过表单传递三个参数列表
  • 这三个列表,浮点数列表、字符串列表和整数列表,必须传递给其他路由以处理信息

有什么好办法吗

您可以通过url参数从主页传递用户输入。i、 e您可以将用户输入的所有参数作为参数附加到接收方url中,并在接收方url端检索它们。请在下面找到相同的样本流:

from flask import Flask, request, redirect

@app.route("/homepage", methods=['GET', 'POST'])
def index():
    ##The following will be the parameters to embed to redirect url.
    ##I have hardcoded the user inputs for now. You can change this
    ##to your desired user input variables.
    userinput1 = 'Hi'
    userinput2 = 'Hello'

    redirect_url = '/you_were_redirected' + '?' + 'USERINPUT1=' + userinput1 + '&USERINPUT2=' + userinput2
    ##The above statement would yield the following value in redirect_url:
    ##redirect_url = '/you_were_redirected?USERINPUT1=Hi&USERINPUT2=Hello'

    return redirect(redirect_url)

@app.route("/you_were_redirected", methods=['GET', 'POST'])
def redirected():
    ##Now, userinput1 and userinput2 can be accessed here using the below statements in the redirected url
    userinput1 = request.args.get('USERINPUT1', None) 
    userinput2 = request.args.get('USERINPUT2', None)
return userinput1, userinput2

使用
会话
对象。会话不需要注册-它们使用Cookie在所有请求中保持用户的唯一性。谢谢。我将再次尝试,有很多方法比通过字符串连接附加查询参数更好。。。