简体   繁体   English

在另一个 function 中使用来自 function 的变量,与 flask

[英]Use a variable from a function in another function, with flask

Im using Flask for making a website.我使用 Flask 来制作网站。

@app.route('/consulta',methods=['POST'])
def consulta():
    if request.method=='POST': #Si se han enviado datos
        dia = request.form['dia'] #Obtenemos los datos del formulario
        videobeam = request.form['videobeam']
        tablero = request.form['tablero']
        hora = request.form['hora']
        aa = darHora(hora)

        a = aa[0] #<<<<<----- HERE IS PROBLEM
        b = aa[1] #<<<<<<- HERE IS PROBLEM


        cursor = db.connection.cursor()
        cursor.execute("SELECT * FROM salones WHERE {0}=1 AND videobeam = '{2}' AND tablero = '{1}' AND `{3}` = 1 AND `{4}` = 1".format(dia,videobeam,tablero,a,b)) 

        #Buscamos que coincidan con la base de datos, se pregunta por el dia de disponibilidad, si tiene videobeam y tablero
        data = cursor.fetchall() #Se obtiene en una lista
        cursor.close() #Se cierra la conexión
        return render_template('consulta.html', datos = data) #Se visualizará los resultados, y se pasa a data como parametro

I would like to use variables 'a' and 'b' from that function, in another function, becase they came from the input from the user in a form.我想在另一个 function 中使用来自 function 的变量“a”和“b”,因为它们来自用户以表单形式输入。 The problem is that i cannot "return" them, because flask only allows me to return the render_template for that function.问题是我不能“返回”它们,因为 flask 只允许我返回 function 的 render_template。

Any idea?任何想法? Thank you!!!谢谢!!!

Assuming the other function is a view function associated with a different endpoint, you can simply pass these variables using Flask session. Assuming the other function is a view function associated with a different endpoint, you can simply pass these variables using Flask session. Eg:例如:

from flask import session

@app.route('/consulta',methods=['POST'])
def consulta():
    if request.method=='POST': #Si se han enviado datos
        dia = request.form['dia'] #Obtenemos los datos del formulario
        videobeam = request.form['videobeam']
        tablero = request.form['tablero']
        hora = request.form['hora']
        aa = darHora(hora)

        session['a'] = aa[0] #<<<<<----- HERE IS PROBLEM
        session['b'] = aa[1] #<<<<<<- HERE IS PROBLEM
        ...

 @app.route('/something')
 def user_parameters():
    a = session.get('a')
    b = session.get('b')
    ...

One way of handling this would be to make a and b global variables:处理此问题的一种方法是创建ab全局变量:

a = None
b = None

@app.route('/consulta',methods=['POST'])
def consulta():
    if request.method=='POST': #Si se han enviado datos
        ...
        global a, b
        a, b = aa[0], aa[1]
        ...

Now, every time consulta() gets called, the global values of a and b are replaced with new ones.现在,每次调用consulta()时,都会将ab的全局值替换为新值。 Elsewhere in the program, you can do the same thing in order to get the most recently-set values of a and b .在程序的其他地方,您可以执行相同的操作以获取ab的最新设置值。


Note that, if you're encountering this problem, you might want to reconsider why you need the values of a and b to act like this.请注意,如果您遇到此问题,您可能需要重新考虑为什么需要ab的值来执行此操作。 Are they tied to the specific user who submitted the POST request?它们是否与提交 POST 请求的特定用户相关联? How are you using them in the other function, and when does the other function run relative to this one?您如何在其他 function 中使用它们,以及其他 function 何时相对于这个运行?

If you need to access the same information between various disconnected API calls, but you have a way (such as a token) to track which user is making the request, I would recommend using an actual database to store information.如果您需要在各种断开连接的 API 调用之间访问相同的信息,但您有办法(例如令牌)来跟踪发出请求的用户,我建议使用实际数据库来存储信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM