Flask Internal Redirect with url_for
-
redirect
-
url_for
-
url_for('login') Returns the url based on the functions name
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route('/')
def index():
return '<a href="/goto">Go to</a>'
@app.route('/goto')
def goto():
return redirect(url_for('user_page'))
@app.route('/user')
def user_page():
return 'User page'
import app
def test_app():
web = app.app.test_client()
rv = web.get('/')
assert rv.status == '200 OK'
assert rv.data == b'<a href="/goto">Go to</a>'
rv = web.get('/goto')
assert rv.status == '302 FOUND'
assert rv.headers['Location'] == 'http://localhost/user'
assert b'<p>You should be redirected automatically to target URL: <a href="/user">/user</a>' in rv.data
rv = web.get('/user')
assert rv.status == '200 OK'
assert rv.data == b'User page'