Skip to content

Support using the route decorator on view classes#3975

Closed
greyli wants to merge 2 commits intopallets:mainfrom
greyli:class-route
Closed

Support using the route decorator on view classes#3975
greyli wants to merge 2 commits intopallets:mainfrom
greyli:class-route

Conversation

@greyli
Copy link
Copy Markdown
Contributor

@greyli greyli commented Apr 28, 2021

Make route as a shortcut for as_view() method when you don't need to pass class arguments:

from flask.views import MethodView

@app.route('/users/', endpoint='users')
class UserAPI(MethodView):

    def get(self):
        users = User.query.all()
        ...

    def post(self):
        user = User.from_form_data(request.form)
        ...

Or

from flask.views import View

@app.route('/users/', endpoint='show_users')
class ShowUsers(View):

    def dispatch_request(self):
        users = User.query.all()
        return render_template('users.html', objects=users)

Checklist:

  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

@davidism
Copy link
Copy Markdown
Member

davidism commented Feb 8, 2022

After thinking about this more, I'm not going to merge it. I don't want to encourage users to use View or MethodView without arguments. It's never going to be as efficient as writing the views directly, since it has to create an instance and call dispatch every request. I think it also misses the intention of View classes, which I think is to act as factories for different configurations of a set of views.

@davidism davidism closed this Feb 8, 2022
@greyli greyli deleted the class-route branch February 9, 2022 04:32
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

route decorator on MethodView classes

3 participants