瓶中模板引擎的使用方法

  介绍

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

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

<强> 1瓶默认的模板引擎是Jinja2

目录结构:

/application.py/模板
  ,,,/oscuser.html 

<强> 2,实例

 application.py
  #=utf - 8编码
  时间=__author__  & # 39; duanpeng& # 39;
  ,
  ,
  import  MySQLdb
  得到,flask  import ,烧瓶,请求,render_template,会话,重定向,url_for,,逃跑
  app =,瓶(__name__ static_folder=& # 39;静态# 39;,static_url_path=& # 39;/静态# 39;)
  ,
  ,
  #定义首页
  @app.route (& # 39;/& # 39;
  def  hello_world ():
  ,,,,user_agent =, request.headers.get(& # 39;用户代理# 39;)
  ,,,,return  & # 39;欢迎!,,你browser  is  % & # 39; %, user_agent
  ,
  ,
  404 #定义错误页面
  @app.errorhandler (404)
  def  not_found(错误):
  ,,,return  render_template (& # 39; error.html& # 39;), 404年
  ,
  ,
  ,
  #定义动态页面
  @app.route (& # 39;/user/& lt; username> & # 39;)
  def  show_user_profile(用户名):
  ,,,#,show 从而user  profile  for  that 用户
  ,,,return  & # 39; User  % & # 39; %,用户名
  ,
  #限制请求方式
  @app.route (& # 39;/sayhello # 39;方法=[& # 39;文章# 39;])
  def  sayHello ():
  ,,,,return “你好,who 断开连接;你吗?“
  #限制请求只能为得到方式
  @app.route(& # 39;/触摸# 39;方法=[& # 39;得到# 39;])
  def 联系():
  ,,,,return  render_template (& # 39; bank.html& # 39;)
  ,
  ,
  #我的账号页面,与数据库交互,实现动态数据处理
  @app.route (& # 39;/myaccount& # 39;方法=[& # 39;得到# 39;])
  def  mydata ():
  ,,,试一试:
  ,,,,,,,#加载驱动,连接数据库,,,,host →ip 港口→端口
  ,,,,,,,conn =, MySQLdb.connect(主机=& # 39;192.168.1.124& # 39;,用户=& # 39;根# 39;,passwd=& # 39;六边形abcdef # 39;, db=& # 39;六边形abcdef # 39;,端口=3306,charset=& # 39; gb2312 # 39;)
  ,,,,,,,cursor =, conn.cursor ()
  ,,,,,,,cursor.execute (“select  *,得到osc_users  t  where  t.login_name =, & # 39; rainbow07693& # 39;“)
  ,,,,,,,,,=,result  cursor.fetchone ()
  ,,,,,,,印刷(结果[4])
  ,,,,,,,cursor.close ()
  ,,,,,,,conn.close ()
  ,,,,,,,return  render_template (& # 39; oscuser.html& # 39;,用户信息=结果)
  ,,,except  MySQLdb.Error, e:
  ,,,,,,,print  e
  ,
  ,
  ,
  if  __name__ ==, & # 39; __main__ # 39;:
  app.run (debug=True) 

以上是“瓶中模板引擎的使用方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

瓶中模板引擎的使用方法