官术网_书友最值得收藏!

Introduction

In Flask, we can write a complete web application without the need of any third-party templating engine. For example, have a look at the following code; this is a simple Hello World application with a bit of HTML styling included:

from flask import Flask
app = Flask(__name__)

@app.route('/')
@app.route('/hello')
@app.route('/hello/<user>')
def hello_world(user=None):
    user = user or 'Shalabh'
    return '''
<html>
    <head>
      <title>Flask Framework Cookbook</title>

    </head>
      <body>
        <h1>Hello %s!</h1>
        <p>Welcome to the world of Flask!</p>
      </body>
</html>''' % user

if __name__ == '__main__':
    app.run()

Is the preceding pattern of writing the application feasible in the case of large applications that involve thousands of lines of HTML, JS, and CSS code? Obviously not!

Here, templating saves us because we can structure our view code by keeping our templates separate. Flask provides default support for Jinja2, although we can use any templating engine as suited. Furthermore, Jinja2 provides many additional features that make our templates very powerful and modular.

主站蜘蛛池模板: 高清| 页游| 恩施市| 青阳县| 安福县| 贵南县| 商河县| 乌拉特中旗| 阿城市| 勃利县| 上高县| 卓尼县| 连平县| 平定县| 谢通门县| 宁陵县| 遵化市| 鸡东县| 榆林市| 惠水县| 惠安县| 盘锦市| 隆林| 古蔺县| 南投市| 济阳县| 孝感市| 巨鹿县| 原阳县| 英超| 平远县| 亳州市| 霍州市| 岳阳县| 禹城市| 东城区| 德江县| 鄂伦春自治旗| 鄯善县| 曲水县| 阿拉尔市|