Plug-in view;
Description: The flask view is inspired by Django's class-based and not function-based generic views, primarily to address the already implemented parts of multiple view functions, inheriting from class inheritance to other views, in short, by writing less code, and then by Add_url_ The rule lets us define a view class that supports dynamic insertion, which is known as the plug-in view
In-depth view:
# Before conversion:
#!/usr/bin/env python# -*- coding: utf-8 -*-" "" ## authors: limanman# 51ctobg: http://xmdevops.blog.51cto.com/# purpose:# "" "# Description: Import Common Module from flask import flask, render_template# Instructions: import other modules app = flask (__name__) @app. Route ('/about ') def web_about (): return render_ Template ('/web/about.html ') @app. Route ('/usr_manager ') Def usr_manager (): usrs = [u ' Li full '] return render_template (' web/usr/manager.html ', usrs=usrs) @ App.route ('/grp_manager ') def grp_managr (): grps = [u ' admin '] return render_template (' web/grp/manager.html ', grps=grps) if __name__ == ' __main__ ': app.run (host= ' 0.0.0.0 ', port=9000, debug=true)
Description: As the above three view function code is basically similar, is to get the data rendering template or direct rendering template, we can try to adapt to more models and templates, more flexible, first to be converted into Class View through the interpolation view subclass inheritance
# After conversion:
#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman# 51ctobg: http://xmdevops.blog.51cto.com/# purpose:# "" "# Description: Import public module from flask import flask, render_template, jsonifyfrom flask.views import view# Description: Import other modules App = flask (__name__) Class baseview (View): def __init__ (self, template_name): self.template_name = Template_name def get_objects (self): return {} def render_template (Self, context): return render_template (Self.template_name, **context) def dispatch_request (self): context = {' objects ': self.get_objectS ()} return self.render_template (context) class Usrmanager (Baseview): def get_objects (self): return [u ' Li full ']class grpmanager (Baseview): def get_objects ( Self): return [u ' admin ']app = flask (__name__) App.add_url_rule ('/about ', view_func=baseview.as_view (' web_about ', template_name= ' web/about.html ')) App.add_url_rule ('/web/usr/manager ', view_func=usrmanager.as_view (' Usr_manager ', template_name= ' web/ Usr/manager.html ') app.add_url_rule ('/web/grp/manager ', view_func=grpmanager.as_view (' Grp_manager ', Template_name= ' web/grp/manager.html ') @app. Route ('/') Def index (): return Jsonify ({' All_url_map ': app.url_map.__str__ ()}) if __name__ == ' __main__ ': app.run (host= ' 0.0.0.0 ', port=9000, debug=true)
Description: Using a class-based plug-in view first declares a subclass that inherits from Flask.views.View and must implement a method that dispatch_request the dispatch request, returning the original response data in the dispatch request, if it is to be added App.url_ The map table needs to bind the URL rule to the view function through the app.add_url_rule (self, *args, **kwargs) method, and since the binding is a view function, it must not be bound directly to subclasses. The base class Flask.views.View provides us with a. As_view (name, *args, **kwargs) class method, inherited we can directly call to generate a view function named name, when the request conforms to the URL rules will be to_ through the converter Python data is handled by APP.URL_MAP to the corresponding view function, which may be a decorated function, or a function generated through a plug-in view, and the resulting returned result is decorated in the response adorner to eventually return to the client
extension: If you want to restrict the HTTP method, you can declare the class attribute methods=[' GET ', ' POST ', or the order Get/post protocol directly in the base class Baseview or subclass . , if you want to handle the Get/post protocol separately, you only need the base class to inherit Flask.views.MethodView and then implement the Get/post method in the subclass, and then you can not provide the methods class properties. It automatically handles get or post requests as defined in the subclass
#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman# 51ctobg: http://xmdevops.blog.51cto.com/# purpose:# "" "# Description: Import public module from flask import flask, render_template, jsonify, requestfrom flask.views import methodview# Description: Import other modules App = flask (__name__) Class baseview (Methodview): def __init__ (Self, template_name): Self.template_name = template_name def render_template (self, Context): return render_template (self.template_name, **context) def dispatch_request (self): objects = getattr (Self, request.method.lower ()) () context = {' ObjecTS ': objects} return self.render_template (context) Class usrmanager (Baseview): def get (self): return [u ' Li full '] def post (self): return [u ' Liu Jianjin ']class grpmanager (baseview, methodview): def get (self): return [u ' anonymous '] def post (self): return [u ' admin ']app = flask (__name__) app.add_url_rule ('/about ', view_func=baseview.as_view (' Web_about ', Template_name= ' web/about.html ')) app.add_url_rule ('/web/usr/manager ', view_func=usrmanager.as_view (' Usr_ Manager ', template_name= ' web/usr/manager.html ')) app.add_url_rule ('/web/grp/manager ', view_func= Grpmanager.as_view (' Grp_manager ', templaTe_name= ' web/grp/manager.html ')) @app. Route ('/') Def index (): return jsonify ( Help (App.add_url_rule)) if __name__ == ' __main__ ': app.run (host= ' 0.0.0.0 ') , port=9000, debug=true)
Extension: Flask also supports operations such as permission checking, login validation, and so on, before running the view function, because the view class is ultimately generated by the. As_view generation of the view function, so the view class is added with no OVA and can only be done on the. As_view. New version of the direct support base class attribute decorators list, define adorner list
#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman# 51ctobg: http://xmdevops.blog.51cto.com/# purpose:# "" "# Description: Import public module from flask import flask, render_template, jsonify, request, session, abortfrom flask.views import methodview# Description: Import other modules App = flask (__name__) def user_ Required (func): def wrapper (*args, **kwargs): uid = session.get (' id ', none) If not uid: abort (401) return func (*args, **kwargs) return wrapperclass baseview (Methodview): decorators = [user_required] def&nbsP;__init__ (self, template_name): self.template_name = template_name def render_template (Self, context): return render_template (Self.template_name, **context) def dispatch_request (self): objects = getattr (Self, request.method.lower ()) () context = {' objects ': objects} return self.render_ Template (context) Class usrmanager (Baseview): def get (self): return [u ' Li full '] def post (self): return [u ' Liu Jianjin ']class grpmanager (BaseView, MethodView): def get (Self): return [u ' anonymous '] def Post (self): return [u ' admin ']app = flask (__name__) App.add_url_rule ('/about ', view_func=baseview.as_view (' web_about ', template_name= ' web/about.html ')) App.add_url_rule ('/web/usr/manager ', view_func=usrmanager.as_view (' Usr_manager ', template_name= ' web/ Usr/manager.html ') app.add_url_rule ('/web/grp/manager ', view_func=grpmanager.as_view (' Grp_manager ', Template_name= ' web/grp/manager.html ') @app. Route ('/') Def index (): return Jsonify (Help (App.add_url_rule)) if __name__ == ' __main__ ': app.run (host= ') 0.0.0.0 ', port=9000, debug=true)
Description: Flask provides us with the decorators class property to set the adorner list, but it can also be manually modified before app.add_url_rule View_func = user_required (Baseview.as_view (' Web_about ', template_name= ' web/about.html '), this time View_func has been modified to add a validation function
This article is from "@Can up and No BB ..." Blog, be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1866526
Site Backend _python+flask.0010.flask The custom view class and view decoration of the plug-in view?