栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Flask 怎样向模板HTML页面传递参数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flask 怎样向模板HTML页面传递参数

如果希望向HTML页面传递参数,可以在函数中申明一个变量,然后使用render_template()函数传递过去。在render_template()函数中将视图函数中的变量赋值给HTML页面中的变量。然后通过模板渲染,就实现传值。

如下所示:

@app.route('/hello.html') 
	def hello_world(): 
	args = request.args # 获取所有参数 
	name =args.get('name') 
	return render_template('hello.html',name=name) 

说明:render_template()中参数名name,指的是模板HTML页面中变量名,值name为在视图函数中定义的变量名,可以命名一样,如:

@form.route('/formplay')  #在蓝图对象上进行操作,路由
def index():
    # 往模板中传入的数据
    my_str11 = 'Hello Word22'
    my_int = 10
    my_array = [3, 4, 2, 1, 7, 9]
    my_dict = {
        'name': 'xiaoming',
        'age': 18
    }
    return render_template('hello.html',
                           my_str=my_str,
                           my_int=my_int,
                           my_array=my_array,
                           my_dict=my_dict
                           )

备注:也可以命名不一样。如下变量my_str22为html页面中的变量名,my_str11为视图函数中定义的变量名,将视图函数中的变量赋值给HTML页面中的变量。

@form.route('/formplay')  #在蓝图对象上进行操作,路由
def index():
    # 往模板中传入的数据
    my_str11 = 'Hello Word22'
    my_int = 10
    my_array = [3, 4, 2, 1, 7, 9]
    my_dict = {
        'name': 'xiaoming',
        'age': 18
    }
    return render_template('hello.html',
                           my_str22=my_str11,
                           my_int=my_int,
                           my_array=my_array,
                           my_dict=my_dict
                           )
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/958194.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号