Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 TypeError:url_for()接受1个位置参数,但给出了2个_Python_Python 3.x_Flask_Url For - Fatal编程技术网

Python TypeError:url_for()接受1个位置参数,但给出了2个

Python TypeError:url_for()接受1个位置参数,但给出了2个,python,python-3.x,flask,url-for,Python,Python 3.x,Flask,Url For,如果x=俄亥俄州 我想将用户重定向到/weather/ohoio 我能去工作的只是/weather/?x=俄亥俄州 我这样做是为了运行第二条路径@app.route(“/weather/”)。我不确定我错过了什么。这是加载/weather/ohoi的最佳方式,其中ohoi是从表单加载的变量 @app.route("/weather/", methods = ['POST', 'GET']) def weather(): if request.method == 'POST':

如果x=俄亥俄州

我想将用户重定向到/weather/ohoio

我能去工作的只是/weather/?x=俄亥俄州

我这样做是为了运行第二条路径@app.route(“/weather/”)。我不确定我错过了什么。这是加载/weather/ohoi的最佳方式,其中ohoi是从表单加载的变量

    @app.route("/weather/", methods = ['POST', 'GET'])
def weather():
    if request.method == 'POST':
        x = request.form['search_location']
        return redirect(url_for('weather', x=x))
        #print (y)
    else:
        return render_template("weather.html")

如果我去掉x=out,我会得到错误“TypeError:url_for()接受1个位置参数,但给出了2个”

它接受变量的关键字参数:

url_for('add', variable=x)

如果我正确理解你的问题,你想要一条动态路线吗

看看这里

@app.route(“/weather/”,方法=['POST','GET'])

您需要第二个端点带有一个path变量,并为()提供
url\u
与端点关联的函数名:

@app.route(“/weather”,methods=[“POST”,“GET”])
def weather():
如果request.method==“POST”:
x=请求。表格[“搜索位置”]
返回重定向(url\u用于(“天气位置”,x=x))
其他:
返回渲染模板(“weather.html”)
@附件路线(“/weather/”)
def天气_位置(x):
返回“It's everys sunny in{}”。格式(x)

看一看,可能会更清楚一些。

我填写了一个表单,我希望表单数据以/weather/的形式传递到url中,因此如果ohio被输入到我希望重定向到的/weather/ohio表单中。从该表单中,将您需要的变量传递到我刚才向您展示的路由,然后将其传递到您的函数。查看我链接的教程,并阅读flask文档,该文档非常好地解释了这一点。我在重定向中没有看到任何变量示例问题是,当我需要的时候,如果需要任何帮助,请考虑使用URL,将其标记为这样,以使其他具有相同问题的人能够找到解决方案,结果是问题是返回重定向(URLION(“天气”,x= x))。指的是错误的函数,应该是weather而不是weather。
@app.route("/weather/<variable>", methods = ['POST', 'GET'])