1,content_processor
- Context processor must return a dictionary, the dictionary
key
will be variable as a template to render - Context processor returns a dictionary, all pages can all be used
- Modified this decorator hook function must return a dictionary, even if have to return empty
Landing time will display the user name.
from flask import Flask,render_template,g,url_for,request,redirect,session import os app = Flask(__name__) app.config['SECRET_KEY'] = os.urandom(24) @app.route('/') def hello_world(): # print("index") return 'index!' @app.route('/login/' ,methods=['GET','POST']) def login(): # print("login") if request.method == "GET": return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') if username == "1" and password == "1": session['username ' ] = " . 1 " return ' Success in Login to! ' the else : return the redirect (the url_for ( ' Login ' )) @ app.route ( ' / Edit / ' ) DEF Edit (): ##. 1 and # 2 are It is the same meaning, but a more convenient, the hasattr () function is used to determine whether the object contains the corresponding attribute. IF the hasattr (G, ' username ' ): # . 1 # IF . the session GET ( ' username ' ) == " . 1 " : return the render_template ( ' edit.html ' ) the else : return the redirect (the url_for ( ' Login ' )) # before_request: executing, executed prior to the function executed before the view request. # Before_request just a decorator, he may want to set the hook function into the code to be executed before the view function is executed. app.before_request @ DEF my_before_request (): Print ( ' hw ' ) # username prove there is a user login status IF the session. GET ( ' username ' ): g.username = the session. GET ( ' username ') #G? # In each view corresponds to function (e.g., ' /', / Login / before performing this function will be executed). @ App.context_processor DEF mycp (): return { " username " : " . 1 " } IF the __name__ == ' __main__ ' : app.run ()