博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flask render_template函数
阅读量:4189 次
发布时间:2019-05-26

本文共 1103 字,大约阅读时间需要 3 分钟。

目录


 

描述

render_template()函数是flask函数,它从模版文件夹templates中呈现给定的模板上下文。

 

语法及参数

import flaskflask.render_template(template_name, **context)

⚠️ render_template()函数需要调用flask包

名称 含义 备注
template_name 模板文件名 字符串型参数,不可省略
context 模板参数 由模板参数和对应的值组成的字典,可以省略的参数

 

返回值

str。render_template()函数返回替换模板参数后的模板文本。

 

使用示例

模板中没有参数

模板../templates/hello_world.html如下:

    
Hello world

Hello World!

render_template使用示例:

import flaskapp = flask.Flask(__name__)@app.route("/hello")def hello():    return flask.render_template("hello_world.html")if __name__ == '__main__':    app.run()

运行后在浏览器中输入,结果如下:

 

给模版传递参数

当模板中存在可变参数时,render_template()函数可以为模板传递参数:

模板../templates/for.html如下:

    
Jinja2 Circulation Control

{
{product}} list:

    {% for product in products %}
  • {
    {product}}
  • {% endfor %}

render_template使用示例:

import flaskapp = flask.Flask(__name__)@app.route("/")def index():    products = ["iphoneX", "MacBook Pro", "Huawei"]    kwargs = {        "products": products    }    return flask.render_template("for.html", **kwargs)if __name__ == '__main__':    app.run()

运行后在浏览器中输入,结果如下:

 

转载地址:http://ycsoi.baihongyu.com/

你可能感兴趣的文章
EasyJF第一次网下交流会成功召开
查看>>
EasyJWeb、RoR、JSF&Struts2,谁更Easy?
查看>>
spring源码分析-XmlBeanFactory导读
查看>>
英雄会的郁闷与收获
查看>>
一个开源组织者的感言2
查看>>
为何不使用spring、struts2、easyjweb等开源框架
查看>>
Eclipse 误删文件怎么办
查看>>
EasyJF开源从网上走到网下
查看>>
让spring帮助你在MVC层解决JPA的缓迟加载问题
查看>>
在EasyJWeb使用spring容器
查看>>
EasyJWeb中灵活的多国语言支持
查看>>
理想·环境·开源
查看>>
EasyJWeb中缺省URL映射转换器揭密
查看>>
EasyJWeb+prototype
查看>>
EasyJF开源重组见闻-1
查看>>
基于Ajax+J2EE的MicroERP源码下载
查看>>
在EasyJWeb中轻松开发Ajax运用
查看>>
EasyJWeb中的代码生成体验
查看>>
EasyDBO-0.9.1版发布
查看>>
开源2007,我们来了
查看>>