bootstrap中文网:https://www.bootcss.com/

1.安装:pip install flask-bootstrap 或者 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple flask-bootstrap

2.初始化

from flask import Flask   #导入FLask类
from flask import render_template #导入render_template,叫渲染模板,帮助把想要的渲染文件给展示出来
from flask_bootstrap import Bootstrap #导入Bootstrap

#Flask类接收一个参数__name__
app = Flask(__name__) #初始化了一个对象,叫app
bootstrap = Bootstrap(app) #初始化flask_bootstrap



#首页
@app.route('/user/<name>')
def user(name): #函数的名字可以随便起,只要符合python函数的命名规则就行
return render_template('bs/user.html',
name=name
)





#Flask应用程序实例的run方法启动web服务器
if __name__ == '__main__':
bootstrap.run()

3.user.html:

{% extends "bootstrap/base.html" %}
{% block title %}
首页
{% 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=".natbar-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="{{ url_for('user',name=name) }}">Flask</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="{{ url_for('user',name=name) }}">Home</a>
</li>

</ul>

</div>

</div>

</div>
{% endblock %}

{% block content %}
<div class="container">
<div class="page-header">
<h1>Hello,{{ name }}</h1>
</div>

</div>
{% endblock %}

  

4.

posted on 2021-01-10 10:07  大话人生  阅读(887)  评论(0编辑  收藏  举报