flask中模板引擎的使用方法

发布时间:2021-04-27 14:21:04 作者:小新
来源:亿速云 阅读:207

小编给大家分享一下flask中模板引擎的使用方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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

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

目录结构:

/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
 
 
 
#定义动态页面
@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中模板引擎的使用方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Flask_学习笔记_09: Flask中的继承
  2. layui模板引擎的使用方法有那些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

flask

上一篇:JavaScript中match函数如何匹配数组

下一篇:python函数超时怎么自动退出

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》