- Flask Framework Cookbook(Second Edition)
- Shalabh Aggarwal
- 92字
- 2021-06-24 13:58:10
There's more...
Now, many of us may be considering whether it would be possible to just declare GET and POST methods inside a View class and let Flask handle the rest of the stuff. The answer to this question is MethodView. Let's write our previous snippet using MethodView:
from flask.views import MethodView
class GetPostRequest(MethodView): def get(self): bar = request.args.get('foo', 'bar') return 'A simple Flask request where foo is %s' % bar def post(self): bar = request.form.get('foo', 'bar') return 'A simple Flask request where foo is %s' % bar app.add_url_rule( '/a-request', view_func=GetPostRequest.as_view('a_request') )