重定向到Flask中的URL [英] Redirecting to URL in Flask

查看:142
本文介绍了重定向到Flask中的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和Flask的新手,我正在尝试执行 Response.redirect 中的等效命令,如 C# - 即:重定向到一个特定的网址 - 我怎么去这个?

这是我的代码:

 导入os 
从flask导入Flask
$ b $ app = Flask(__ name__)

@ app.route '$'
def hello():
return'Hello World!'

if __name__ =='__main__':
#绑定到PORT,
port = int(os.environ.get('PORT',5000))
app.run(host ='0.0.0.0',port = port)


解决方案

您必须返回重定向

 导入os 
从烧瓶导入烧瓶,重定向

app =烧瓶(__ name__)

@ ():
def hello():
return redirect(http://www.example.com,code = 302)

if __name__ =='__main__':
#绑定到PORT如果defi ned,否则默认为5000.
port = int(os.environ.get('PORT',5000))
app.run(host ='0.0.0.0',port = port)

参见烧瓶文档

代码的默认值是302,所以 code = 302 可以被省略或被其他重定向代码(301,302,303,305和307中的一个)替换。

I'm new to Python and Flask and I'm trying to do the equivalent of Response.redirect as in C# - ie: redirect to a specific URL - how do I go about this?

Here is my code:

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

解决方案

you have to return a redirect

import os
from flask import Flask,redirect

app = Flask(__name__)

@app.route('/')
def hello():
    return redirect("http://www.example.com", code=302)

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

see the documentation on flask docs.

default value for code is 302 so code=302 can be omitted or replaced by other redirect code (one in 301, 302, 303, 305, and 307)

这篇关于重定向到Flask中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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