zoukankan      html  css  js  c++  java
  • flask 快速入门-06 之 `渲染模板`

    渲染模板

    你可以使用方法 render_template() 来渲染模版。所有你需要做的就是提供模版的名称以及 你想要作为关键字参数传入模板的变量。这里有个渲染模版的简单例子:

    from flask import render_template
    
    @app.route('/hello/')
    @app.route('/hello/<name>')
    def hello(name=None):
        return render_template('hello.html', name=name)
    

    Flask将会在templates文件夹中寻找hello.html模板,并将name这个变量传入模板。

    如果你的应用是个模块,这个templates文件夹在模块的旁边。

    Case 1: a module:

    /application.py
    /templates
        /hello.html
    

    如果它是一个包,那么这个文件夹在你的包里面。

    Case 2: a package:

    /application
        /__init__.py
        /templates
            /hello.html
    

    读取/var/log/messages 内容,并打印在网页上。

    #coding=utf-8
    from flask import Flask, render_template
    app = Flask(__name__)
    
    @app.route('/')
    def messages():
        f = open('/var/log/messages','r')
        all = f.readlines()
        return render_template('messages.html', messages=all)
    
    if __name__ == "__main__":
        app.run(host='0.0.0.0', port=80, debug=True)
    

    /templates 目录下写一个messages.html文件。

    <!doctype html>
    <title>logfile for messages</title>
      <h6>{{ messages }}!</h6>
  • 相关阅读:
    C#新特性
    蛋清打发奶油状
    VS 2015 开发Android底部导航条----[实例代码,多图]
    使用微软的MSBuild.exe编译VS .sln .csproj 文件
    双色球基础分析--SQL
    Windows 7 中的 God Mode
    Free Online SQL Formatter
    Windows 特殊文件夹
    常用DNS列表(电信、网通)
    C语言词法分析:C#源码
  • 原文地址:https://www.cnblogs.com/itflycat/p/4453361.html
Copyright © 2011-2022 走看看