- url_for(‘login’, key=value) Pass parameters to the URL path keys that are not part of the route definitions will be converted to query string values
from flask import Flask, request, redirect, url_for
app = Flask(__name__)
@app.get('/')
def main_page():
return '''<form action="/goto" method="POST">
<input name="username">
<input type="submit" value="Go">
</form>'''
@app.post('/goto')
def login_post():
username = request.form.get('username')
if username is None or username == '':
return redirect(url_for('user_page_central'))
return redirect(url_for('user_page', name = username))
@app.get('/user/')
def user_page_central():
return 'List of users'
@app.get('/user/<name>')
def user_page(name):
return f'Page of {name}'
import redirect_parameters
import pytest
@pytest.fixture()
def web():
return redirect_parameters.app.test_client()
def test_main_page(web):
rv = web.get('/')
assert rv.status == '200 OK'
assert b'<form action="/goto" method="POST">' in rv.data
def test_goto_post_data(web):
rv = web.post('/goto', data={'username': 'Joe'})
assert rv.status == '302 FOUND'
assert rv.headers['Location'] == '/user/Joe'
assert b'<p>You should be redirected automatically to the target URL: <a href="/user/Joe">/user/Joe</a>' in rv.data
def test_goto_post(web):
rv = web.post('/goto')
assert rv.status == '302 FOUND'
assert rv.headers['Location'] == '/user/'
assert b'<p>You should be redirected automatically to the target URL: <a href="/user/">/user/</a>' in rv.data
def test_user_jane(web):
rv = web.get('/user/Jane')
assert rv.status == '200 OK'
assert rv.data == b'Page of Jane'
def test_user(web):
rv = web.get('/user/')
assert rv.status == '200 OK'
assert rv.data == b'List of users'