[关闭]
@1kbfree 2018-05-16T15:11:06.000000Z 字数 2096 阅读 1030

3.模板渲染和参数传递

flask


首先创建一个templates文件夹,用于存放模板的(不懂百度)。

image_1cd4fuj84d1n11bh14ao1rnj136j2d.png-28.2kB

image_1cd4g7jg617qruai1vs0c6e14mf2q.png-16.2kB

index.html代码:
image_1cd4gbnlve8nn9a1mhf1cuiclg37.png-54.4kB

app的代码:

  1. from flask import Flask,render_template
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. return render_template('index.html') #如果访问了127.0.0.1就会打开这个index.html文件,然后呈现在页面上
  6. if __name__ == '__main__':
  7. app.run(debug=True,host='127.0.0.1',port=80)

image_1cd4ggfk653r1b5c11fo140h1ch83k.png-148.8kB

模板参数传递

  1. from flask import Flask,render_template
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. return render_template('index.html',name = 'iamfree',age = 16) #就是说把这里的内容传递到html里
  6. if __name__ == '__main__':
  7. app.run(debug=True,host='127.0.0.1',port=80)

index.php文件代码:
image_1cd4huisf11prnms3tsh718o2m.png-63.3kB
结果:
image_1cd4go5su1vkj58a32m1lltf8741.png-152.1kB

那么这里有一个问题,就是我们要传递给模板的参数多了,我们该怎么办呢,下面来看下操作

  1. from flask import Flask,render_template
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. context = {}
  6. context['name'] = 'iamfree'
  7. context['age'] = 16
  8. return render_template('index.html',**context) #这里里的**context等价于name=iamfree,age=16
  9. if __name__ == '__main__':
  10. app.run(debug=True,host='127.0.0.1',port=80)

代码中**的用法:
image_1cd4hjlhpvet1lca1pip1kbavao9.png-28.2kB

传递对象给模板

  1. from flask import Flask,render_template
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. context = {}
  6. class Obj():
  7. name = 'iamfree'
  8. age = 16
  9. o = Obj
  10. context['obj'] = o
  11. return render_template('index.html',**context) #就是说把这里的内容传递到html里
  12. if __name__ == '__main__':
  13. app.run(debug=True,host='127.0.0.1',port=80)

image_1cd4i6ien1adr4v31j1rbq01dg13.png-70kB

模板语句

  1. from flask import Flask,render_template
  2. app = Flask(__name__)
  3. @app.route('/index/<option>')
  4. def index(option):
  5. context = {}
  6. context['option'] = option
  7. return render_template('index.html',**context)
  8. if __name__ == '__main__':
  9. app.run(debug=True,host='127.0.0.1',port=80)

模板语句:if判断

image_1cd4jauvji6e190j34u4rm191c5n.png-84.3kB
image_1cd4ivdgt1id01fr71ph72eo12h21t.png-97kB
image_1cd4j0ido13jq1idi1h0r8s51loq4a.png-115.7kB

模板for循环

  1. from flask import Flask,render_template
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. users = {
  6. 'name':'iamfree',
  7. 'age':16
  8. }
  9. return render_template('index.html',users=users)
  10. if __name__ == '__main__':
  11. app.run(debug=True,host='127.0.0.1',port=80)

image_1cd595ho51eg66gr1j671d8ufpq9.png-74.7kB

结果:
image_1cd595ubo1q6m1m2gm6s14gb1mpbm.png-140.7kB

过滤器

过滤器格式: 变量名|过滤器

{ var | reverse}} 对var进行反转
{{ "hello world" | reverse | upper }} 把helloword反转后,在全部大写

列表操作

  1. first:取第一个元素
  2. <p>{{ [1,2,3,4,5,6] | first }}</p>
  1. last:取最后一个元素
  2. <p>{{ [1,2,3,4,5,6] | last }}</p>
  1. length:获取列表长度
  2. <p>{{ [1,2,3,4,5,6] | length }}</p>
  1. sum:列表求和
  2. <p>{{ [1,2,3,4,5,6] | sum }}</p>
  1. sort:列表排序
  2. <p>{{ [6,2,3,1,5,4] | sort }}</p>

image_1cd4k6l151b017ok6k013m41ts1p.png-1811.6kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注