Flask URL / Redirection with Blueprint

Redirection

Prefix the URL endpoint with the name of the blueprint and a dot (.): weather.update

Url_for() with parameter

Pass parameter: location

@weather.route('/', methods=['POST', 'GET'])
def index():
    form = WeatherForm()
    session_id = request.cookies.get('session')
    if form.validate_on_submit():
        if form.wrong_data.data:
            return redirect(url_for('weather.update', location=location))
    else:
        return render_template('index.html', form=form)


@weather.route('/update/<location>', methods=['POST', 'GET'])
def update(location):
    form = UpdateForm()
    form.location.data = location
    if form.validate_on_submit():
    ...

Use url_for in html

  • static
<link href="{{ url_for('static', filename='styles.css') }}" rel="stylesheet">
  • form action
<form class="form-horizontal" action="{{ url_for('weather.update', location=form.location.data) }}" method = "post">

results matching ""

    No results matching ""