Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Multiple URL rules pointing to lambdas in the same scope do not work #585

Closed
boomlinde opened this Issue Aug 31, 2012 · 3 comments

Comments

Projects
None yet
2 participants

I'm dynamically generating a bunch of views in a piece of code that essentially boils down to

for manager in managers:
    app.add_url_rule('/managers/%s/<parameter>' % manager['identifier'], manager['lambda']

where the lambda key in the manager objects is a lambda object using a small set of common functions with varying input parameters based on values in the manager object.

The issue is that when adding multiple url rules pointing to lambdas in the same module, all the routes will point to the lambda that was defined last. Upon further investigation, it seems Flask URL rules only refer to endpoint functions by name in a string, so all the endpoints that point to lambdas in the same scope will end up pointing at the same lambda, referred to as "package.".

I realize that keeping names is necessary for reverse lookup of URL rules, but I think that ideally Flask should keep track of the endpoint function object itself by a pointer as well and use that to call.

Owner

untitaker commented Aug 31, 2012

Not sure if that's the problem, but your second manager access could either be identified as part of the formatting thing, or as second parameter for add_url_rule

Owner

untitaker commented Aug 31, 2012

Also, according to the documentation for add_url_rule, you have the wrong number of arguments passed to add_url_rule.

for manager in managers:
    app.add_url_rule('/managers/%s/<parameter>' % manager['identifier'], manager['identifier'], manager['lambda'])

I realize now that I have overlooked some things, and this was of course not a bug in Flask. Thank you for the help untitaker!

I should be more careful when trying to boil things down! At a different place in the code I generate the identifier parameter myself, simply copying function.name. Replacing that with str(function) resolved my issue. I feel dumb.

@boomlinde boomlinde closed this Aug 31, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment