Flask中视图函数传数据到前端页面

1.将字符串传到前端页面

   在user.py中做简单修改即可Flask中视图函数传数据到前端页面@pythonweb_blue.route('/user') def user(): data = '我是小可爱' return render_template('index.html',data = data)
Flask中视图函数传数据到前端页面

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>{{data}}</h1>
    </body>
    </html>

2.传多个数据时,先把数据写成字典之后再传入,举例说明。

Flask中视图函数传数据到前端页面

 @pythonweb_blue.route('/user')
    def user():
        context2 = {
            'username': 'abc',
            'age': 18,
            'sex': '男'
        }
        return render_template('index.html',**context2)```

index.html

  <html lang="en">
  <head>
      <meta charset="UTF-8">
      <title>Title</title>
  </head>
  <body>
  <h1>{{username}}</h1>
  </body>
  </html>