模版
业务逻辑和表现逻辑
把表现逻辑单独的放到模版中去,提升可维护性
Jinja2 模板
模版的一种,包含响应文本的文件
渲染模版
<h1>Hello world</h1>
<h1>Hello, {{ name }} !</h1>
from flask import Flask, render_template
@app.route('/')
def index():
return render_template('index.html')
@app.route('/user/<name>')
def user(name):
return render_template('user.html', name=name)
变量
- 变量
{{ name }}
- 变量过滤器
- capitalize
{{ name|capitalize }}
- safe: 渲染时不转义
- low
- upper
- title: 值中每个单词的首字母都转为大写
- trim
- striptags:渲染之前把值中所有的HTML标签都删掉
- capitalize
控制结构
- if else结构
{% if user %}
Hello, {{ user }}
{% else %}
Hello, Stranger!
{% endif %}
- for 循环
<ul>
{% for comment in comments %}
<li>{{ comment }}</li>
{% endfor %}
</ul>
- macro -宏
{% macro render_comment(comment) %}
<li>{{ commont }}</li>
{%endmacro%}
<ul>
{% for comment incomments %}
{{ render_comment(comment) }}
{% endfor %}
</ul>
#使用宏
{% import 'macros.html' as macros %}
<ul>
{% for comment in comments %}
{{ macros.render_comment(comment) }}
{% endfor %}
</ul>
- 重复使用的片段可以放在单独的文件中然后在使用的地方include
{% include 'common.html' %}
- 模版继承
#base.html
<html>
<head>
{% block head %}
<title>{% block title %}{% endblock %} - My Application</title>
{% blockend %}
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
#继承模版
{% extends "base.html" %}
{% block title %} Index {% endblock %}
{% block head %}
{{ super() }}
<style> </style>
{% endblock %} {% block body %}
<h1> Hello, World!</h1>
{% endblock %}
Twitter Bootstrap
- install
pip install flask-bootstrap
- 初始化
from flask.ext.bootstrap import Bootstrap
bootstrap = Bootstrap(app)
- 使用
{% extends "bootstrap/base.html" %}
{% block head %}
{{ super() }}
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico')}}" type="image/x-icon">
<link rel ='icon' href="{{ url_for('static', filename='favicon.ico')}}" type="image/x-icon">
{% endblock %}
{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %}
{% block title %}Flasky{% endblock %}
{% block navbar %}
<div class='navbar navbar-inverse' role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Flasky</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
</ul>
</div>
</div>
</div>
{% endblock %}
{% block content %}
<div class="container">
{% block page_content %}{% endblock %}
</div>
{% endblock %}
自定义错误页面
- 404:客户端请求未知页面或路由
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
- 500:有未处理异常时显示
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
链接
使用url_for()生成动态地址
- url_for('user',name='john', _external=True)
结果为 http://localhost:5000/user/john - url_for('index',page=2)
http://localhost:5000/?page=2
Flask-Moment 本地化日期和时间
- install
pip install flask-moment
- init
from flask.ext.moment import Moment
moment = app(Moment)
- html中的使用
<p>
The local date and time is {{ moment(current_ time).format(' LLL') }}.</p> <p>
That was {{ moment(current_time).fromNow(refresh=True) }}
</p>