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

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.

主站蜘蛛池模板: 抚州市| 瑞金市| 仲巴县| 柯坪县| 宜良县| 鹤岗市| 工布江达县| 罗城| 六盘水市| 龙口市| 连山| 商丘市| 海伦市| 东海县| 儋州市| 江西省| 文安县| 东明县| 龙州县| 镇远县| 无为县| 孝昌县| 布尔津县| 大连市| 长泰县| 凤台县| 林甸县| 台州市| 柳江县| 正宁县| 庆阳市| 桂林市| 烟台市| 温州市| 镇远县| 从化市| 嘉祥县| 宝山区| 昭苏县| 定日县| 福鼎市|