Flask Bootstrap起步
pip3 install flask-script
pip3 install flask-bootstrap
hello.py
from flask import Flask, render_template
from flask_script import Manager
from flask_bootstrap import Bootstrap
app = Flask(__name__)
manager = Manager(app)
bootstrap = Bootstrap(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/user/')
def user(name):
return render_template('user.html', name=name)
if __name__ == '__main__':
manager.run()
user.html
{% extends "bootstrap/base.html" %}
{% 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 navigationspan>
<span class="icon-bar">span>
<span class="icon-bar">span>
<span class="icon-bar">span>
button>
<a class="navbar-brand" href="/">Flaskya>
div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Homea>li>
ul>
div>
div>
div>
{% endblock %}
{% block content %}
<div class="container">
<div class="page-header">
<h1>Hello, {{ name }}!h1>
div>
div>
{% endblock %}
base.html
# 集成自bootstrap/base.html
{% extends "bootstrap/base.html" %}
{% 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 navigationspan>
<span class="icon-bar">span>
<span class="icon-bar">span>
<span class="icon-bar">span>
button>
<a class="navbar-brand" href="/">Flaskya>
div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Homea>li>
ul>
div>
div>
div>
{% endblock %}
{% block content %}
<div class="container">
{% block page_content %}{% endblock %}
div>
{% endblock %}
user.html
{% extends "base.html" %}
{% block title %}Flasky{% endblock %}
{% block page_content %}
<div class="page-hearder">
<h1>Hello, {{ name }}h1>
div>
{% endblock %}
自定义错误页面 404.html
# 继承自定义base.html
{% extends "base.html" %}
{% block title %}Flasky-Page Not Found {% endblock %}
{% block page_content %}
<div class="page-header">
<h1>Not Foundh1>
div>
{% endblock %}
moment.js可以在浏览器渲染日期和事件,Flask-moment是一个Flask扩展,可以把moment.js集成到Jinja模板中
pip install flask-moment
在template/base.html中引入moment.js
{% block script %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %}
hello.py中加入moment = Moment(app)
即
from flask import Flask, render_template
from flask_script import Manager
from flask_bootstrap import Bootstrap
from datetime import datetime
from flask_moment import Moment
app = Flask(__name__)
moment = Moment(app)
manager = Manager(app)
bootstrap = Bootstrap(app)
@app.route('/')
def index():
return render_template('index.html', current_time=datetime.utcnow())
@app.route('/user/')
def user(name):
return render_template('user.html', name=name)
if __name__ == '__main__':
manager.run()
index.html
{% extends "base.html" %}
{% block content %}
{{ super() }}
<h1>Hello the local date and time is {{ moment(current_time).format('LLLL') }}h1>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}p>
{% endblock %}
发现时间总是不能正常显示,查看页面代码发现 时间的样式表中style="display:none"
改为style="display:block"
即可正常显示
效果如下图所示
本文参考《Flask Web开发-基于Python的Web应用开发实战》