3

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?

6
  • Apparently url_for() cannot find the views.show_view registration. Are you sure your views Blueprint uses the name views when registered with your Flask object? Commented Aug 17, 2015 at 19:24
  • Or is views perhaps your Flask object itself? Commented Aug 17, 2015 at 19:25
  • @MartijnPieters: I am sure that is the case. I have views = Blueprint(__name__, __name__). The app object is named app Commented Aug 17, 2015 at 19:31
  • __name__ is the name of the current module, so your Blueprint object is probably not named views. Commented Aug 17, 2015 at 19:38
  • 1
    I suspect that views.py is a module in a package, in which case __name__ will contain the full path. Don't use __name__ as the first argument, give it a string literal instead so you can be sure of its value. Commented Aug 17, 2015 at 20:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.