Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pluggable Method Views broken by fix for #570 #794

Closed
ajford opened this issue Jul 7, 2013 · 8 comments
Closed

Pluggable Method Views broken by fix for #570 #794

ajford opened this issue Jul 7, 2013 · 8 comments

Comments

@ajford
Copy link

ajford commented Jul 7, 2013

So I'm not sure if this is meant to happen, but when you attempt to use MethodViews as described in the docs http://flask.pocoo.org/docs/views/#method-views-for-apis, I get an Assertion Error generated by the app.add_url_rule added in 661ee54. This commit was created to fix #570.

The MethodView example in the docs calls for registering multiple routes to the same class, which causes an AssertionError to be raised.

Is there another way to create a method based set of views without causing this error to be raised? Would it be better to switch from an AssertionError to a Warning that could be ignored?

@rplopes
Copy link

rplopes commented Jul 10, 2013

Same problem here, as referenced in #796. Some code works for 0.9 and breaks in 0.10.

@untitaker
Copy link
Contributor

I suppose a quick fix for this would be to call MethodView.as_view for each add_url_rule, since this will generate a separate function for each route.

Ignore me.

@untitaker
Copy link
Contributor

Actually this has been fixed with 44e39ab

@ajford
Copy link
Author

ajford commented Jul 17, 2013

Ok, after digging around, I figured out the problem.

If you call MethodView.as_view for each add_url_rule, the unique views for each route cause the Assertion error to be raised.

If you call foo = MethodView.as_view() then use foo for each of the add_url_rule calls, it will not cause the Assertion error. See https://gist.github.com/ajford/6023148

If you run app.py and app2.py against flask 0.8, flask 0.10, you'll see that in flask 0.8, both files run, while in flask 0.10+, only app2.py works.

Not an obvious complication. One I glossed over each time I reviewed the code and the Docs concerning add_url_rule and the classed based views.

@mitsuhiko
Copy link
Contributor

Working as intended. If you use class based views and you assign more than one rule to the same handler you need to actually use the same handler. eg: create the view function once, bind it to a local variable and then assign it multiple times. The old behavior of silently ignoring it was dangerous as it did not inform you that you created and destroyed your handler multiple times and only the last one won.

@michaelbukachi
Copy link

Hello, I seem to be getting this error with the latest version of flask(0.11). I've tried ajford's solution but its still not working.

@davidism
Copy link
Member

@michaelbukachi that solution (creating the view once and adding rules with it multiple times) is correct. If you have questions about your own code please ask on Stack Overflow with a Minimal, Complete, and Verifiable Example.

@michaelbukachi
Copy link

Sorry about that. Found the bug in my code. I feel so silly:)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants