Redirect to new page with path and optional argument in Flask and Flask-WTF not working

Question:

I am using Flask and Flask-WTF and I have the following code in my views.py file:

from flask import render_template, flash, redirect, url_for   
from . import app, forms

@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
    form = forms.HelpSearch()
    if form.validate_on_submit():
        flash('Searched for: %s' % form.value.data)
        redirect(url_for('help', form.value.data))
    return render_template('index.html', title='Index', form=form)


@app.route('/help/<keyword>', methods=['GET', 'POST'])
def help(keyword=None):
    form = forms.HelpSearch()
    if form.validate_on_submit():
        flash('Searched for: %s' % form.value.data)
        redirect(url_for('help', keyword=form.value.data))

    # This is just some dummy data for testing my template
    keywords = ['n', 'north']
    groups = ['movement']
    syntax = [
        {'cmd':"n", 'args': ''},
        {'cmd':'north', 'args': ''}
    ]
    content = 'Move north'

    return render_template('show_help.html',
                            title=keyword,
                            form=form,
                            keywords=keywords,
                            groups=groups,
                            syntax=syntax,
                            content=content)

What I want, and expect, it to do is that when someone puts some text in the forms search field and hits the search button it returns that value and then I redirect to the appropriate page, e.g. they search for foo and end up at /help/foo.

Sadly the redirect from the form validation bit is not redirecting as desired. It just appears to be reloading the current page.

I know for a fact that the form is getting and returning the data because the flash call is showing up with the correct info, e.g. 'Searched for: foo' but when I pass the keyword to url_for the page, again, simply reloads. Manually navigating to /help/foo works fine.

I have tested that url_for is working and it creates the appropriate path as desired when I enter a keyword manually, e.g. print url_for('help', keyword='foo') prints /help/foo.

Anyone have any idea why it is not redirecting as desired?

Edit: Got it running on Heroku if anyone wants to see what exactly is happening.

Asked By: Chris Bond

||

Answers:

I think your problem is for not returning anything

you can check this:

def index():
    form = forms.HelpSearch()
    if form.validate_on_submit():
        flash('Searched for: %s' % form.value.data)
        return redirect(url_for('help', keyword=form.value.data))
    return render_template('index.html', title='Index', form=form)
Answered By: Mohammad Efazati

The problem is in how you are doing your redirects. Instead of this:

redirect(url_for('help', keyword=form.value.data))

do this:

return redirect(url_for('help', keyword=form.value.data))

The redirect() function does not raise an exception like abort() does, it just returns a Response object that needs to be passed up the stack.

Answered By: Miguel Grinberg
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.