我想做的是类似于回应。重定向作为在c# -即:重定向到一个特定的URL -我怎么去做这个?

这是我的代码:

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)

当前回答

从Flask API文档(v. 2.0.x):

flask.redirect(location, code=302, Response=None) Returns a response object (a WSGI application) that, if called, redirects the client to the target location. Supported codes are 301, 302, 303, 305, and 307. 300 is not supported because it’s not a real redirect and 304 because it’s the answer for a request with a request with defined If-Modified-Since headers. New in version 0.6: The location can now be a unicode string that is encoded using the iri_to_uri() function. Parameters: location – the location the response should redirect to. code – the redirect status code. defaults to 302. Response (class) – a Response class to use when instantiating a response. The default is werkzeug.wrappers.Response if unspecified.

其他回答

在Flask中有两种方法可以重定向到URL。

例如,您希望在用户登录后将其重定向到另一个路由,等等。 你可能还想将用户重定向到期望一些变量示例的路由:@app.route('/post/<string:post_id>')

好吧,实现flask重定向案例# 1,它很简单,只需做:

from flask import Flask,redirect,render_template,url_for
app = Flask(__name__)


@app.route('/login')
def login():
    # if user credentials are valid, redirect to user dashboard
    if login == True:
       return redirect(url_for(app.dashboard))
    else:
       print("Login failed !, invalid credentials")
    return render_template('login.html',title="Home Page")


@app.route('/dashboard')
def dashboard():
    return render_template('dashboard.html',title="Dashboard")

要实现案例#2的flask重定向,请执行以下操作

from flask import Flask,redirect,render_template,url_for
app = Flask(__name__)


@app.route('/home')
def home():
    # do some logic, example get post id
    if my_post_id:
       # **Note:** post_id is the variable name in the open_post route
       # We need to pass it as **post_id=my_post_id**
       return redirect(url_for(app.open_post,post_id=my_post_id))
    else:
       print("Post you are looking for does not exist")
    return render_template('index.html',title="Home Page")


@app.route('/post/<string:post_id>')
def open_post():
    return render_template('readPost.html',title="Read Post")

同样的事情也可以在视图中完成

<a href="{{url_for(app.open_post,post_id=my_post_id)}}"></a>

请注意:重定向时总是使用app.home或app.something..(路由或视图函数名)而不是使用重定向("/home")。 原因是,如果您出于某种原因将路由示例从“/home”修改为“/index/page”,那么您的代码将会中断

你可以这样用:

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
     # Redirect from here, replace your custom site url "www.google.com"
    return redirect("https://www.google.com", code=200)

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)

下面是该代码的引用链接。

从Flask API文档(v. 2.0.x):

flask.redirect(location, code=302, Response=None) Returns a response object (a WSGI application) that, if called, redirects the client to the target location. Supported codes are 301, 302, 303, 305, and 307. 300 is not supported because it’s not a real redirect and 304 because it’s the answer for a request with a request with defined If-Modified-Since headers. New in version 0.6: The location can now be a unicode string that is encoded using the iri_to_uri() function. Parameters: location – the location the response should redirect to. code – the redirect status code. defaults to 302. Response (class) – a Response class to use when instantiating a response. The default is werkzeug.wrappers.Response if unspecified.

flask.redirect(location, code=302)

文档可以在这里找到。

Flask包含用于重定向到任何url的重定向函数。此外,你可以用abort的错误代码提前中止请求:

from flask import abort, Flask, redirect, url_for

app = Flask(__name__)

@app.route('/')
def hello():
    return redirect(url_for('hello'))

@app.route('/hello'):
def world:
    abort(401)

默认情况下,每个错误代码都会显示一个黑白错误页面。

重定向方法默认接受代码302。http状态代码列表。