python基础flask——bootstrap模板
2022-08-03 09:57:03
106
{{single.collect_count}}

困于下衡于虑而后作
今天的学习目标是bootstrap模板的使用
模板能减少很多工作量,想搭建网站,模板必不可少,可以节约时间来研究逻辑部分
首先是官方文档,boostrap文档
打开网页后,先下载实例
在这里插入图片描述

1.使用bootstrap模板
下载完成之后,打开第一个示例的html,将这个html复制到项目文件夹的templates中
输入如下代码:

from flask_bootstrap import Bootstrapfrom flask import Flaskfrom flask import render_templatefrom datetime import datetimeapp = Flask(__name__)bootstrap = Bootstrap(app)print(bootstrap)@app.route('/')def hello():return render_template('index.html')app.run(debug=True)

打开链接即可看见显示的界面
其实index页面是可以对base里面的html进行继承的,继承就相当于使用模板

{% extends "bootstrap/base.html" %}{% block title %}Flask{% endblock %}

当然,可以定义块的内容
2.自定义错误页面

from flask import Flaskfrom flask import render_templateapp = Flask(__name__)@app.errorhandler(404)def page_not_found(e):print(e)return render_template('404.html'), 404@app.errorhandler(500)def page_not_found(e):print(e)return render_template('404.html'), 500app.run(debug=True) 

3.使用flask-moment本地化时间和日期
安装:

pip install flask-moment

代码:

from date import datetime@app.route('/')def hello():return render_template('index.html',current_time=datetime.utcnow())app.run()

index.html中的代码

 <h1>hello word</h1><p>本地数据时间{{moment(current_time).format('LLL')}}</p><p>时间{{moment(current_time).fromNow(refresh=True)}}</p>

书中给的这段html代码时间显示不出来,不知道是怎么回事,找不到答案,暂时先留下这个问题
下节准备学习flask的web表单

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}