如何在Flask中使用参数调用另一条路线? [英] How can we call one route from another route with parameters in Flask?

查看:66
本文介绍了如何在Flask中使用参数调用另一条路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个带有两个输入的表单发送一个POST请求到Flask路由.

 < form action =" http://localhost:5000/xyz"method ="POST">< p> x<输入类型=文字"名称="x";/></p>< p> y<输入类型=文本"名称="y"/></p>< p><输入类型=提交"值=提交";/></p></form> 

Flask代码是这样的.

  @ app.route('/xyz',方法= ['POST','GET'])def xyz():如果request.method =='POST':x = request.form ["x"]y = request.form ["y"]one法(x,y)返回render_template('index.html',var1 = var1,var2 = var2)#abc(x,y)#我可以像这样调用abc().我想立即调用abc(),因为它在控制台中流式传输callonemethod(x,y)的日志.@ app.route('/abc',方法= ['POST','GET'])def abc():Callanothermethod(x,y)返回render_template('index.html',var1 = var3,var2 = var4)#我想在这里使用x,y.我每次调用xyz()时也想调用abc() 

如何使用Flask中的参数从另一条路线调用一条路线?

解决方案

您有两个选择.

选项1 :
使用从路由中获得的参数进行重定向.

如果您有这条路线:

  import os从flask导入Flask,重定向,url_for@ app.route('/abc/< x>/< y>')def abc(x,y):Callanothermethod(x,y) 

您可以像这样重定向到上述路线:

  @ app.route('/xyz',方法= ['POST','GET'])def xyz():如果request.method =='POST':x = request.form ["x"]y = request.form ["y"]one法(x,y)返回重定向(url_for('abc',x = x,y = y)) 

另请参阅有关Flask中重定向的文档./p>

选项2:
似乎从多个不同位置调用了 abc 方法.这可能意味着将其重构为视图之外的一个好主意:

在utils.py

来自其他模块的

 导入调用另一种方法def abc(x,y):Callanothermethod(x,y) 

在应用程序/查看代码中:

  import os从flask导入Flask,重定向,url_for从utils import abc@ app.route('/abc/< x>/< y>')def abc_route(x,y):Callanothermethod(x,y)abc(x,y)@ app.route('/xyz',方法= ['POST','GET'])def xyz():如果request.method =='POST':x = request.form ["x"]y = request.form ["y"]one法(x,y)abc(x,y) 

I am sending a POST request from one form with two inputs to a Flask route.

  <form action = "http://localhost:5000/xyz" method = "POST">
     <p>x <input type = "text" name = "x" /></p>
     <p>y <input type = "text" name = "y" /></p>
     <p><input type = "submit" value = "submit" /></p>
  </form>

The Flask code is like this.

@app.route('/xyz', methods = ['POST', 'GET'])
def xyz():
    if request.method == 'POST':
       x = request.form["x"]
       y = request.form["y"]
       callonemethod(x,y)
    return render_template('index.html', var1=var1, var2=var2)
       #abc(x,y) #can i call abc() like this .i want to call abc() immediately, as it is streaming log of callonemethod(x,y) in console.

@app.route('/abc', methods = ['POST', 'GET'])       
def abc():
    callanothermethod(x,y)
    return render_template('index.html', var1=var3, var2=var4)
    #I want to use that x, y here. also want to call abc() whenever i call xyz()

How can I call one route from another route with parameters in Flask?

解决方案

You have two options.

Option 1:
Make a redirect with the parameters that you got from the route that was called.

If you have this route:

import os
from flask import Flask, redirect, url_for

@app.route('/abc/<x>/<y>')
def abc(x, y):
  callanothermethod(x,y)

You can redirect to the route above like this:

@app.route('/xyz', methods = ['POST', 'GET'])
def xyz():
    if request.method == 'POST':
       x = request.form["x"]
       y = request.form["y"]
       callonemethod(x,y)
       return redirect(url_for('abc', x=x, y=y))

See also the documentation about redirects in Flask

Option 2:
It seems like the method abc is called from multiple different locations. This could mean that it could be a good idea to refactor it out of the view:

In utils.py

from other_module import callanothermethod
def abc(x, y):
  callanothermethod(x,y)

In app/view code:

import os
from flask import Flask, redirect, url_for
from utils import abc

@app.route('/abc/<x>/<y>')
def abc_route(x, y):
  callanothermethod(x,y)
  abc(x, y)

@app.route('/xyz', methods = ['POST', 'GET'])
def xyz():
    if request.method == 'POST':
       x = request.form["x"]
       y = request.form["y"]
       callonemethod(x,y)
       abc(x, y)

这篇关于如何在Flask中使用参数调用另一条路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆