当前位置: 移动技术网 > IT编程>脚本编程>Python > Python编程flask使用页面模版的方法

Python编程flask使用页面模版的方法

2019年01月04日  | 移动技术网IT编程  | 我要评论

在flask中可以像go和angular那样使用页面模版(template),可以将html页面显示进行模版化,通过参数传递与页面进行数据交互。

概要信息

事前准备:flask

liumiaocn:flask liumiao$ which flask
/usr/local/bin/flask
liumiaocn:flask liumiao$ flask --version
flask 1.0.2
python 2.7.10 (default, jul 15 2017, 17:16:57) 
[gcc 4.2.1 compatible apple llvm 9.0.0 (clang-900.0.31)]
liumiaocn:flask liumiao$

代码示例:嵌入式的html模版

像angular一样,我们可以在flask中写前端的页面,python代码中混杂着html代码,在这里将前面的helloworld示例进行简单的修改,将显示的hello world加上的设置。

代码示例

liumiaocn:flask liumiao$ cat flask_1.py 
#!/usr/bin/python
from flask import flask
app = flask(__name__)
@app.route("/")
def hello():
  return "<h1>hello world!</h1>"
if __name__ == "__main__":
  app.debug=true
  app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$

执行&确认

在helloworld示例中我们提到有两种方式启动flask的微服务进程,这里再添加一种,添加#!/usr/bin/python之后,同时对此文件添加可执行权限比如755,即可使用.启动

liumiaocn:flask liumiao$ chmod 755 flask_1.py 
liumiaocn:flask liumiao$ ./flask_1.py 
 * serving flask app "flask_1" (lazy loading)
 * environment: production
  warning: do not use the development server in a production environment.
  use a production wsgi server instead.
 * debug mode: on
 * running on http://0.0.0.0:7000/ (press ctrl+c to quit)
 * restarting with stat
 * debugger is active!
 * debugger pin: 131-533-062

通过curl进行结果确认:

liumiaocn:flask liumiao$ curl http://www.lhsxpumps.com/_localhost:7000
<h1>hello world!</h1>liumiaocn:flask liumiao$

页面确认

代码示例

上面的示例过于简单,写一个简单的完整的页面来确认一下

liumiaocn:flask liumiao$ cat flask_1.py 
#!/usr/bin/python
from flask import flask
app = flask(__name__)
@app.route("/")
def hello():
  return '<!doctype html> \
<html> \
<head> \
<meta charset="utf-8"> \
<title>hello</title> \
</head> \
<body>\
<h1>hello world!</h1> \
</body>\
</html>'
if __name__ == "__main__":
  app.debug=true
  app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$

执行&确认

通过curl可以确认页面范围信息

liumiaocn:flask liumiao$ ./flask_1.py 
 * serving flask app "flask_1" (lazy loading)
 * environment: production
  warning: do not use the development server in a production environment.
  use a production wsgi server instead.
 * debug mode: on
 * running on http://0.0.0.0:7000/ (press ctrl+c to quit)
 * restarting with stat
 * debugger is active!
 * debugger pin: 131-533-062

也可以通过浏览器来确认title和页面显示

页面模版

嵌在python的代码中非常的麻烦,转义的连接,以及源码的查看都非常不方便。flask提供了jinja2的模版渲染,只需要引入render_template即可使用。

import render_template

为了使用这个功能,首先需要在程序中做如下import

from flask import render_template

准备页面信息

比如将上文中嵌入的html页面独立成,详细信息如下:

liumiaocn:flask liumiao$ ls templates/

liumiaocn:flask liumiao$ cat templates/ 
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>hello template</title>
</head>
<body>
  <h1>hello world!</h1>
</body>
</html>
liumiaocn:flask liumiao$

注意事项:flask会在当前目录的templates下搜索对应的模版文件,所以需要创建templates文件夹,然后将模版html文件放入其中。

页面调用

在页面上只需要调用render_template即可实现url与对应模版的关联,

render_template(“”)

详细代码

liumiaocn:flask liumiao$ cat flask_2.py 
#!/usr/bin/python
from flask import flask
from flask import render_template
app = flask(__name__)
@app.route("/")
def hello():
  return render_template("")
if __name__ == "__main__":
  app.debug=true
  app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$

执行&确认

liumiaocn:flask liumiao$ python flask_2.py 
 * serving flask app "flask_2" (lazy loading)
 * environment: production
  warning: do not use the development server in a production environment.
  use a production wsgi server instead.
 * debug mode: on
 * running on http://0.0.0.0:7000/ (press ctrl+c to quit)
 * restarting with stat
 * debugger is active!
 * debugger pin: 131-533-062

使用curl可以看到详细的html代码,而且读起来方便多了

liumiaocn:~ liumiao$ curl http://www.lhsxpumps.com/_localhost:7000
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>hello template</title>
</head>
<body>
  <h1>hello world!</h1>
</body>
</html>liumiaocn:~ liumiao$

也可以通过浏览器确认并查看源码

小结

使用render_template,flask也可以像angular一样非常方便的创建用于展示的模版视图,我们已经说过render_template是基于jinja2的模版,在下一篇文章中将继续介绍template的数据交互和控制方式。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网