Python 我们如何使用Flask中的参数从另一个路由调用一个路由?
我正在从一个表单向一个路由发送一个POST请求,其中包含两个输入Python 我们如何使用Flask中的参数从另一个路由调用一个路由?,python,flask,Python,Flask,我正在从一个表单向一个路由发送一个POST请求,其中包含两个输入 <form action = "http://localhost:5000/xyz" method = "POST"> <p>x <input type = "text" name = "x" /></p> <p>y <input type = "tex
<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>
如何使用Flask中的参数从另一个路由调用一个路由?您有两个选项
选项1:使用从调用的路由获得的参数进行重定向 如果您有此路线:
import os
from flask import Flask, redirect, url_for
@app.route('/abc/<x>/<y>')
def abc(x, y):
callanothermethod(x,y)
另请参见Flask中的关于重定向
选项2:方法
abc
似乎是从多个不同的位置调用的。这可能意味着从视图中重构它可能是一个好主意:
在utils.py中
from other_module import callanothermethod
def abc(x, y):
callanothermethod(x,y)
应用程序内/视图代码:
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)
导入操作系统
从烧瓶导入烧瓶,重定向,url_
从UTIL导入abc
@应用程序路径('/abc/')
def abc_路线(x,y):
callanothermethod(x,y)
abc(x,y)
@app.route('/xyz',方法=['POST','GET'])
def xyz():
如果request.method==“POST”:
x=请求。表格[“x”]
y=请求。表格[“y”]
callonemethod(x,y)
abc(x,y)
很高兴看到您的答案,但我将返回并使用这两种方法进行渲染。所以我不能回答,你能检查一下我更新的问题吗。如果可能,尝试更新答案。这会很有帮助的。那么选择2呢?您可以解耦abc()方法并从两个路由中调用。
from other_module import callanothermethod
def abc(x, y):
callanothermethod(x,y)
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)