在咱们对flask的一些引擎应用时,就不得不提到其中的一个默认引擎了。有些初学flask的人对Jinja2还没有应用过,所以不晓得该从何下手。本篇对于这种默认的引擎应用进行了整顿,有对flask模板引擎感兴趣的,能够跟着咱们一起来看看Jinja2的根底操作,具体的内容如下开展。

1、flask默认的模板引擎是Jinja2

目录构造:

/application.py/templates    /oscuser.html

2、实例

application.py#coding=utf-8__author__ = 'duanpeng'  import MySQLdbfrom  flask import  Flask,request,render_template,session, redirect, url_for, escapeapp = Flask(__name__,static_folder='static',static_url_path='/static')  #定义首页@app.route('/')def hello_world():     user_agent = request.headers.get('User-Agent')     return 'welcom! ,you browser is %s' % user_agent  #定义404谬误页面@app.errorhandler(404)def not_found(error):    return render_template('error.html'), 404   #定义动静页面@app.route('/user/<username>')def show_user_profile(username):    # show the user profile for that user    return 'User %s' % username #限度申请形式@app.route('/sayHello',methods=['post'])def sayHello():     return "hello,who are you?"#限度申请只能为get形式@app.route('/touch',methods=['get'])def touch():     return render_template('bank.html')  #我的账号页面,与数据库交互,实现动静数据处理@app.route('/myaccount',methods=['get'])def mydata():    try:        #加载驱动 连贯数据库    host ->ip port->端口        conn = MySQLdb.connect(host='192.168.1.124',user='root',passwd='abcdef',db='abcdef',port=3306,charset='gb2312')        cursor = conn.cursor()        cursor.execute("select * from osc_users t where t.login_name = 'rainbow07693'")        result  = cursor.fetchone()        print(result[4])        cursor.close()        conn.close()        return render_template('oscuser.html',userinfo=result)    except MySQLdb.Error,e:        print e   if __name__ == '__main__':app.run(debug=True)

以上就是flask中模板引擎的应用,置信大家曾经对Jinja2的用法有了肯定的意识。平时在课后也能够找无关的材料进行深刻学习。