新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:flask中模板引擎怎么用?
在我們對flask的一些引擎使用時,就不得不提到其中的一個默認(rèn)引擎了。有些初學(xué)flask的人對Jinja2還沒有使用過,所以不知道該從何下手。本篇對于這種默認(rèn)的引擎使用進行了整理,有對flask模板引擎感興趣的,可以跟著我們一起來看看Jinja2的基礎(chǔ)操作,具體的內(nèi)容如下展開。

創(chuàng)新互聯(lián)公司長期為近千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為修武企業(yè)提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站建設(shè),修武網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
1、flask默認(rèn)的模板引擎是Jinja2
目錄結(jié)構(gòu):
/application.py /templates /oscuser.html
2、實例
application.py
#coding=utf-8
__author__ = 'duanpeng'
import MySQLdb
from flask import Flask,request,render_template,session, redirect, url_for, escape
app = 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
#定義動態(tài)頁面
@app.route('/user/')
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')
#我的賬號頁面,與數(shù)據(jù)庫交互,實現(xiàn)動態(tài)數(shù)據(jù)處理
@app.route('/myaccount',methods=['get'])
def mydata():
try:
#加載驅(qū)動 連接數(shù)據(jù)庫 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中模板引擎的使用,相信大家已經(jīng)對Jinja2的用法有了一定的認(rèn)識。平時在課后也可以找有關(guān)的資料進行深入學(xué)習(xí)。
網(wǎng)頁名稱:創(chuàng)新互聯(lián)Python教程:flask中模板引擎怎么用?
地址分享:http://m.5511xx.com/article/djdphog.html


咨詢
建站咨詢
