zoukankan      html  css  js  c++  java
  • 转:flask之jinja2模板查找路径及关键字传参

    转自:https://www.cnblogs.com/qingbaobei7370/p/11330809.html

    一、Flask渲染Jinja模板

    • jinja2模板介绍和查找路径

      要渲染一个模板,通过render_template方法即可

      ①默认路径 

    复制代码
    from flask import Flask,render_template
    
    #指定templates路径
    app = Flask(__name__)
    
    
    @app.route('/')
    def index():
        return render_template('index.html')if __name__ == '__main__':
        app.run(host='127.0.0.1',port=5000,debug=True)
    复制代码

      ②指定路径 

      如果想更改模板文件地址,应该在创建app的时候,给Flask传递一个关键字参数template_folder,指定具体的路径

    复制代码
    from flask import Flask,render_template
    
    #指定templates路径
    app = Flask(__name__,template_folder=r'D:untitledflask_env	emplates')
    
    @app.route('/')
    #@app.route,是一个装饰器
    #@app.route('/')就是将url中的/映射到hello_world这个视图函数上面
    def index():
        return render_template('index.html')
    
    @app.route('/huashu/')
    def huashu():
        return render_template('posts/huashu.html')
    
    if __name__ == '__main__':
        app.run(host='127.0.0.1',port=5000,debug=True)
    复制代码
    • jinja2模板传参及其技巧

      ①单个关键字传参,代码如下:

      --python--

    复制代码
    from flask import Flask,render_template
    
    #指定templates路径
    app = Flask(__name__,template_folder=r'D:untitledflask_env	emplates')
    
    #传递单个参数
    @app.route('/lvzhi/')
    def lvzhi():
        return render_template('posts/lvzhi.html',name='绿植')
    
    
    if __name__ == '__main__':
        app.run(host='127.0.0.1',port=5000,debug=True)
    复制代码

      --HTML-- 

    复制代码
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>网红绿植</title>
    </head>
    <body>
        <h1>网红绿植</h1>
        花样介绍
        <p>{{name}}</p>
    
    </body>
    </html>
    复制代码

      ②多个关键字传参,代码如下:

      --python--

    复制代码
    from flask import Flask,render_template
    
    #指定templates路径
    app = Flask(__name__,template_folder=r'D:untitledflask_env	emplates')
    
    @app.route('/')
    #@app.route,是一个装饰器
    #@app.route('/')就是将url中的/映射到hello_world这个视图函数上面
    def index():
        return render_template('index.html')
    
    @app.route('/huashu/')
    #传递多个参数
    def huashu():
        context={
            'name':'鲜花',
            'price':'¥215',
            'describe':{
                'variety1':'玫瑰',
                'variety2': '百合'
            }
        }
        return render_template('posts/huashu.html',**context)
    
    #传递单个参数
    @app.route('/lvzhi/')
    def lvzhi():
        return render_template('posts/lvzhi.html',name='绿植')
    
    if __name__ == '__main__':
        app.run(host='127.0.0.1',port=5000,debug=True)
    复制代码

      --HTML-- 

    复制代码
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>鲜花超市</title>
    </head>
    <body>
        <h1>鲜花超市</h1>
        花样介绍
        <p>{{name}}</p>
        <p>{{price}}</p>
        <p>{{describe.variety1}}</p>
        <p>{{describe['variety2']}}</p>
    
    </body>
    </html>
    复制代码
  • 相关阅读:
    duilib框架分析:几个回调(C++11)
    duilib框架分析(一)概述
    图解JVM--(二)垃圾回收
    图解jvm--(一)jvm内存结构
    4 (计算机网络) DHCP与PXE:IP是怎么来的,又是怎么没的?
    3(计算机网络)ifconfig:最熟悉又陌生的命令行
    2 (计算机网络)理解网络协议的工作模式
    1 (计算机网络)我们常用的网络协议有哪些?
    阿里云配置mysql
    深入Spring Boot:那些注入不了的Spring占位符(${}表达式)
  • 原文地址:https://www.cnblogs.com/ohlala/p/11492583.html
Copyright © 2011-2022 走看看