Four .Flask special basics decorator template middleware (use)

A template middleware Flask special decorators

1 .Flask template syntax directly see the effect of surface

https://www.cnblogs.com/lovershowtime/p/11349576.html     templates and Django template similar

edit.html

<form> asdfasdf asdfasdf asdf asdf Ha ah ah ah ah ah ah ah ah ah </ form>
login.html

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h1>模板11111111</h1> {% block content %} {% endblock %} </body> </html>
show.html 

{% the extends "the login.html"%} {#} incorporated inherited template syntax # {%} {#% Block Content Syntax inherited template} # {} {} users.0 {{Users [0]}} { TXT}} { <- {{TXT | Safe}!} -> {{FUNC (. 6) {}}} # single function using # {{SB (1,9) {}} # this is a global decorator decorative directly call parameters passed} # {{. 1 | DB (2,3)}} {#@app.template_filter () must use the so-parameters decorator} # { % IF . 1 | DB (2,3)% } <div> 333 </ div> { % the else % } <div> 999 </ div> { % endif% } { % the include "edit.html"%} {# #} introduce other templates {# inside this Flask macro definitions (equivalent to a function) #} {% macro ccccc(name, type='text', value='') %} <h1>哈哈哈哈</h1> <input type="{{ type }}" name="{{ name }}" value="{{ value }}"> <input type="submit" value="提交"> {% endmacro %} {{ ccccc('n1''N2' {{CCCCC (Here is equivalent to calling} ##)}} {)}} { # Here is equivalent to calling} # {{CCCCC ( ' N3 ' )}} { # here is equivalent to calling} # {%}% endblock

 

app.py 

from
Flask Import the Flask, Request, jsonify, JSON, the render_template, the redirect, the url_for, the session, Markup, Flash, get_flashed_messages App = the Flask ( the __name__ ) # globally defined function @ app.template_global () # decorator decorative function global can not be used to transmit the reference template and the template can be used directly or call DEF SB (A1, A2): # {{SB (1,9)}} return A1 + A2 # globally defined function @ app.template_filter ( ) # decorator filtered decoration function can not be used globally parameter passing to the template and the template can be used directly or call DEF DB (A1, A2, A3): # {{. 1 | DB (2,3)}} parameter passing mode return A1 + A2 + A3 DEFFUNC (Arg): # function takes a single parameter passed to the template return Arg +. 1 @ app.route ( ' / TPL / ' ) DEF TPL (): context = { ' Users ' : [ ' John Doe ' , ' display to Wuji ' , ' la la ' ], ' TXT ' : Markup ( " <INPUT type =" text "/> " ), " FUNC " : FUNC } return the render_template ( 'show.html',**context) if __name__=="__main__": app.run()



2 .Flask Middleware

https://www.cnblogs.com/lovershowtime/p/11384508.html      Django and Flask middleware middleware but not the same

- When starting method call?
            - When a user initiates a request before execution.
        - Task: Before performing call method to do an operation, the operation to make a call after method execution.
from flask import Flask
app = Flask(__name__)
@app.route('/index')
def index():
    print('index')
    return "Index"

class Middleware(object):
    def __init__(self,old):
        self.old = old
    def __call__(self, *args, **kwargs):
        ret = self.old(*args, **kwargs)
        return ret

if __name__ == '__main__':
    app.wsgi_app = Middleware(app.wsgi_app)
    app.run()

 3. Special decorator (intermediate bit to django)

      @app. template_global used in the template used to define global decorator (see above method using the template)
        
     @app. template_filter                   decorator defined in the template used globally
 

 

 
 

@ App.before_request focus

@ App.after_request focus

before_request 没有返回 值的情况下 看图

from
flask import Flask app = Flask(__name__) @app.before_request def aa(): print('aa1') @app.after_request def bb(cc): print('bb22') return cc @app.route("/index/") def index(): print("index") return "index1111" @app.route("/home/") def home(): print("home") return "home22222222" if __name__ == '__main__': app.run()


 
  
In the case of Figure before_request return value
from flask import Flask
app = Flask(__name__)
@app.before_request
def aa():
    print('aa1')
    return "哈哈哈哈1111"

@app.after_request
def bb(cc):
    print('bb22')
    return cc

@app.route("/index/")
def index():
      print("index")

      return "index1111"
@app.route("/home/")
def home():
      print("home")
      return "home22222222"
if __name__ == '__main__':
    app.run()

from flask import Flask
app = Flask(__name__)
@app.before_first_request   不常用 了解
def x1():
    print('123123')

@app.route('/index/')
def index():
    print('index')
    return "Index"

@app.route('/home/')
def order():
    print('order')
    return "order"

@app.errorhandler(404)         不常用 了解
def not_found(arg):
print(arg) return "没找到"

if __name__ == '__main__':
app.run()


 

Guess you like

Origin www.cnblogs.com/lovershowtime/p/11735954.html