Flask redirect to url and pass query strings

Question:

I have a view that is defined like such:

@views.route('/issues')
def show_view():
    action = request.args.get('action')
method = getattr(process_routes, action)
return method(request.args)

in my process_routes module, I would like to call this method, and pass query string values. I have the following code:

return redirect(url_for('views.show_view', action='create_or_update_success'))

I have a function in process_routes named create_or_update_success

I am getting

BuildError: ('views.show_view', {'action': 'create_or_update_success'}, None)

views is a blueprint. I can successfully call

/issues?action=create_or_update_success

In my browser.

What am I doing wrong?

Asked By: Mark

||

Answers:

The first part, views., has to reflect the first argument you give to your Blueprint() object exactly.

Don’t be tempted to set that first argument to __name__, as that is likely to contain the full path of the module when inside a package. In your case I suspect that to be some_package.views rather than just views.

Use a string literal for the Blueprint() first argument instead:

views_blueprint = Blueprint('views', __name__)

so you can refer to url_for('views.show_view') without getting build errors.

Answered By: Martijn Pieters
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.