• Flask02-Template


    ## 基础使用
    
        $ vim app/templates/index.html
        > <html>
        >   <head>
        >     <title>{{title}} - microblog</title>
        >   </head>
        >   <body>
        >       <h1>Hello, {{user.nickname}}!</h1>
        >   </body>
        > </html>
    
        $ vim app/views.py
        > from flask import render_template
        > from app import app
        >
        > @app.route('/')
        > @app.route('/index')
        > def index():
        >     user = { 'nickname': 'Miguel' } # fake user
        >     return render_template("index.html",
        >         title = 'Home',
        >         user = user)
    
    ## 模板中控制语句( if-else for )
    
        $ vim app/templates/index.html
        > <html>
        >   <head>
        >     {% if title %}
        >     <title>{{title}} - microblog</title>
        >     {% else %}
        >     <title>microblog</title>
        >     {% endif %}
        >   </head>
        >   <body>
        >     <h1>Hi, {{user.nickname}}!</h1>
        >     {% for post in posts %}
        >     <p>{{post.author.nickname}} says: <b>{{post.body}}</b></p>
        >     {% endfor %}
        >   </body>
        > </html>   
    
        $ vim app/views.py
        > def index():
        >     user = { 'nickname': 'Miguel' } # fake user
        >     posts = [ # fake array of posts
        >         {
        >             'author': { 'nickname': 'John' },
        >             'body': 'Beautiful day in Portland!'
        >         },
        >         {
        >             'author': { 'nickname': 'Susan' },
        >             'body': 'The Avengers movie was so cool!'
        >         }
        >     ]
        >     return render_template("index.html",
        >         title = 'Home',
        >         user = user,
        >         posts = posts)
    
    ## 模板继承
    
        $ vim app/templates/base.html
        <html>
          <head>
            {% if title %}
            <title>{{title}} - microblog</title>
            {% else %}
            <title>microblog</title>
            {% endif %}
          </head>
          <body>
            <div>Microblog: <a href="/index">Home</a></div>
            <hr>
            {% block content %}{% endblock %}
          </body>
        </html>
    
        $ vim app/templates/index.html
        {% extends "base.html" %}
        {% block content %}
        <h1>Hi, {{user.nickname}}!</h1>
        {% for post in posts %}
        <div><p>{{post.author.nickname}} says: <b>{{post.body}}</b></p></div>
        {% endfor %}
        {% endblock %}
  • 相关阅读:
    Windows系统开机硬盘自检问题解决
    更改软件默认安装路径
    windows添加开机启动项
    xp 如何打开(进行)远程桌面连接
    xp看系统位数
    教你创建一个别人打不开删不掉的文件夹(干坏事爱好者必看)
    无操作一段时间显示器无信号
    长时间用电脑,(给窗口选一个合适的颜色)设置窗口绿色
    Windows右键菜单设置与应用技巧
    Outlook如何定时发邮件
  • 原文地址:https://www.cnblogs.com/mlsec/p/8595275.html
Copyright © 2020-2023  润新知