Flask 列表操作

filter.html  在templates 文件夹下

<!-- 列表操作 -->
{{ [1,2,3,4,5,6] | first }}
<br>
{{ [1,2,3,4,5,6] | last }}
<br>
{{ [1,2,3,4,5,6] | length }}
<br>
{{ [1,2,3,4,5,6] | sum }}
<br>
{{ [1,3,2,5,4,6] | sort }}
<br>
<br/> my_array 原内容:{{ my_array }}
<br/> my_array 反转:{{ my_array | lireverse }}
from flask import Flask,request, jsonify, redirect, url_for,abort,make_response,session,render_template
app = Flask(__name__)

@app.route('/index')
def index():
    my_array =[1,3,45,3536,575]
    return render_template('filter.html',
                           my_array =my_array)
#自定义filter
def do_listrever(li):
    #通过源列表创建一个新列表
    tmp_li = list(li)
    tmp_li.reverse()
    return tmp_li
app.add_template_filter(do_listrever,'lireverse')
if __name__ == '__main__':
    app.run(debug=True)

day1.html在templates文件夹下

{%  if comments | length >10 %}
     There are {{ comments |length }}
{%  else %}
{#     <a href="filter.html"></a>#}
    <a href="https://www.baidu.com">wegr</a><!--链接跳转-->
{% endif %}
<br>
{%  for i in post %}
      {{ post.title }}<br>
       {{ post.text| safe }}<br>
     <!--  {{ post.age }}<br>-->
{%  endfor %}
{%  for it in my_list if it.id!=5 %}
     {%  if loop.index ==1 %}
          <li style="background-color: orange">{{ it.value }}</li>
     {%  elif loop.index ==2 %}
          <li style="background-color: green">{{ it.value }}</li>
     {%  elif loop.index ==3 %}
          <li style="background-color: red">{{ it.value }}</li>
      {% endif %}
{% endfor %}
from flask import Flask,request, jsonify, redirect, url_for,abort,make_response,session,render_template
app = Flask(__name__)

@app.route('/demo1')
def demo1():
    comments = [1,2,3,4,5,3]
    post ={
        'title':'Python',
        'age':'1999',
        'text':'cltt'
    }
    my_list =[
        {
            'id': 1,
            'value': 'happy'
        },
    {
        'id': 2,
        'value': 'birthday'
    },
    {
        'id': 3,
        'value': 'to'
    }
    ]
    return render_template('day1.html',
                           comments = comments,
                           post = post,
                           my_list = my_list)
if __name__ == '__main__':
    app.run(debug=True)

 

 

点击wegr会跳转到百度界面

 

posted on 2020-05-07 10:41  cltt  阅读(670)  评论(0编辑  收藏  举报

导航