<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bootstrap &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/tag/bootstrap/feed/" rel="self" type="application/rss+xml" />
	<link>https://greyli.com</link>
	<description>一个编程和写作爱好者的在线记事本</description>
	<lastBuildDate>Thu, 06 Nov 2025 11:36:11 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>

<image>
	<url>https://greyli.com/wp-content/uploads/2025/03/avatar-500-compressed-144x144.jpg</url>
	<title>Bootstrap &#8211; 李辉 / Grey Li</title>
	<link>https://greyli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>使用Bootstrap-Flask在Flask项目中集成Bootstrap</title>
		<link>https://greyli.com/integrate-bootstrap-in-flask-application-with-bootstrap-flask/</link>
		<comments>https://greyli.com/integrate-bootstrap-in-flask-application-with-bootstrap-flask/#comments</comments>
		<pubDate>Sun, 15 Jul 2018 09:41:55 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Bootstrap]]></category>
		<category><![CDATA[Bootstrap-Flask]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[Flask扩展]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1751</guid>
		<description><![CDATA[Bootstrap-Flask是一个简化在Flask项目中集成前端开源框架Bootstrap过程的Flask扩 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
	<a href="https://github.com/greyli/bootstrap-flask">Bootstrap-Flask</a>是一个简化在Flask项目中集成前端开源框架<a href="https://getbootstrap.com/">Bootstrap</a>过程的Flask扩展。使用Bootstrap可以快速的创建简洁、美观又功能全面的页面，而Bootstrap-Flask让这一过程更加简单和高效。尤其重要的是，Bootstrap-Flask支持最新版本的Bootstrap 4版本。
</p>
<div id="attachment_1761" style="width: 766px" class="wp-caption alignnone"><img class="size-full wp-image-1761 wp-caption alignnone" src="http://greyli.com/wp-content/uploads/2018/07/bootstrap-flask.png" alt="Bootstrap-Flask logo" width="756" height="209" title="" style="" srcset="https://greyli.com/wp-content/uploads/2018/07/bootstrap-flask.png 756w, https://greyli.com/wp-content/uploads/2018/07/bootstrap-flask-150x41.png 150w, https://greyli.com/wp-content/uploads/2018/07/bootstrap-flask-300x83.png 300w, https://greyli.com/wp-content/uploads/2018/07/bootstrap-flask-624x173.png 624w" sizes="(max-width: 756px) 100vw, 756px" /><p class="wp-caption-text">Bootstrap-Flask logo</p></div>
<p>
	<span data-offset-key="aok4p-0-0">GitHub项目主页：</span><a class="Link ztext-link" data-editable="true" data-offset-key="aok4p-1-0" href="https://github.com/greyli/bootstrap-flask" rel="noopener" target="_blank"><span data-offset-key="aok4p-1-0">https://github.com/greyli/bootstrap-flask</span></a>
</p>
<h2>
	和Flask-Bootstrap的区别<br />
</h2>
<p>
	简单来说，Bootstrap-Flask的出现是为了替代不够灵活且缺乏维护的Flask-Bootstrap。它的主要设计参考了Flask-Bootstrap，其中渲染表单和分页部件的宏基于Flask-Bootstrap中的相关代码修改实现。和Flask-Bootstrap相比，前者有下面这些优点：
</p>
<ul>
<li>
		去掉了内置的基模板，换来更大的灵活性，提供了资源引用代码生成函数
	</li>
<li>
		支持Bootstrap 4
	</li>
<li>
		标准化的Jinja2语法
	</li>
<li>
		提供了更多方便的宏，比如简单的分页导航部件、导航链接等
	</li>
<li>
		宏的功能更加丰富，比如分页导航支持传入URL片段
	</li>
<li>
		统一的宏命名，即&ldquo;render_*&rdquo;，更符合直觉
	</li>
</ul>
<h2>
	安装与初始化<br />
</h2>
<p>
	你如果使用过Flask-Bootstrap，那么除了安装时的名称外，这个过程基本没有不同。
</p>
<p>
	安装：
</p>
<pre>
$ pip install bootstrap-flask
</pre>
<p>
	初始化：
</p>
<pre>
from flask_bootstrap import Bootstrap
from flask import Flask

app = Flask(__name__)

bootstrap = Bootstrap(app)</pre>
<p>
	如果你使用工厂函数创建程序实例，那么可以使用下面的方式初始化扩展：
</p>
<pre>
from flask_bootstrap import Bootstrap
from flask import Flask

bootstrap = Bootstrap()

def create_app():
    app = Flask(__name__)
    bootstrap.init_app(app)
    
    return app
</pre>
<h2>
	Bootstrap-Flask提供了哪些功能<br />
</h2>
<h3>
	2个资源加载函数<br />
</h3>
<p>
	在简单的示例程序中，或是在开发时，你可以使用它提供的两个快捷方法来生成Bootstrap资源引用代码，如下所示：
</p>
<pre>
&lt;head&gt;
{{ bootstrap.load_css() }}
&lt;/head&gt;
&lt;body&gt;
...
{{ bootstrap.load_js() }}
&lt;/body&gt;</pre>
<h3>
	7个快捷渲染宏<br />
</h3>
<p>
	目前，Bootstrap-Flask一共提供了7个宏，分别用来快捷渲染各类Bootstrap页面组件，并提供了对扩展Flask-WTF、Flask-SQLAlchemy的支持。
</p>
<table style="height: 270px;">
<thead>
<tr style="height: 27px;">
<th style="height: 27px;">
				宏
			</th>
<th style="height: 27px;">
				模板路径
			</th>
<th style="height: 27px;">
				说明
			</th>
</tr>
</thead>
<tbody>
<tr style="height: 27px;">
<td style="height: 27px;">
				render_field()
			</td>
<td style="height: 27px;">
				bootstrap/form.html
			</td>
<td style="height: 27px;">
				渲染一个WTForms表单字段
			</td>
</tr>
<tr style="height: 27px;">
<td style="height: 27px;">
				render_form()
			</td>
<td style="height: 27px;">
				bootstrap/form.html
			</td>
<td style="height: 27px;">
				渲染一个WTForms表单类
			</td>
</tr>
<tr style="height: 54px;">
<td style="height: 54px;">
				render_pager()
			</td>
<td style="height: 54px;">
				bootstrap/pagination.html
			</td>
<td style="height: 54px;">
				渲染一个简单分页导航，包含上一页和下一页按钮
			</td>
</tr>
<tr style="height: 27px;">
<td style="height: 27px;">
				render_pagination()
			</td>
<td style="height: 27px;">
				bootstrap/pagination.html
			</td>
<td style="height: 27px;">
				渲染一个标准分页导航部件
			</td>
</tr>
<tr style="height: 27px;">
<td style="height: 27px;">
				render_nav_item()
			</td>
<td style="height: 27px;">
				bootstrap/nav.html
			</td>
<td style="height: 27px;">
				渲染一个导航条目
			</td>
</tr>
<tr style="height: 27px;">
<td style="height: 27px;">
				render_breadcrumb_item()
			</td>
<td style="height: 27px;">
				bootstrap/nav.html
			</td>
<td style="height: 27px;">
				渲染一个面包屑条目
			</td>
</tr>
<tr style="height: 54px;">
<td style="height: 54px;">
				render_static()
			</td>
<td style="height: 54px;">
				bootstrap/utils.html
			</td>
<td style="height: 54px;">
				渲染一个资源引用语句，即&nbsp;<code>&lt;link&gt;</code>或<code>&lt;script&gt;</code>标签语句
			</td>
</tr>
</tbody>
</table>
<p>
	使用方法相当简单，以渲染Flask-WTF（WTForms）的表单类的render_form()宏为例，你只需要从对应的模板路径导入宏，然后调用即可并传入必要的参数：
</p>
<pre>
{% from &#39;bootstrap/form.html&#39; import render_form %}

{{ render_form(form) }}</pre>
<p>
	你可以在项目仓库的examples目录下找到一个完整的示例程序，示例程序的运行方式如下：
</p>
<pre>
$ git clone https://github.com/greyli/bootstrap-flask.git
$ pip install flask flask-wtf flask-sqlalchemy bootstrap-flask
$ cd bootstrap-flask/examples
$ flask run</pre>
<p>
	现在访问http://localhost:5000即可看到示例程序主页。示例程序包含所有宏的实际调用示例，其中分页宏示例页面如下图所示：
</p>
<div id="attachment_1760" style="width: 1033px" class="wp-caption aligncenter"><img class="size-full wp-image-1760 wp-caption aligncenter" src="http://greyli.com/wp-content/uploads/2018/07/分页宏示例.png" alt="分页宏示例" width="1023" height="780" srcset="https://greyli.com/wp-content/uploads/2018/07/分页宏示例.png 1023w, https://greyli.com/wp-content/uploads/2018/07/分页宏示例-150x114.png 150w, https://greyli.com/wp-content/uploads/2018/07/分页宏示例-300x229.png 300w, https://greyli.com/wp-content/uploads/2018/07/分页宏示例-624x476.png 624w" sizes="(max-width: 1023px) 100vw, 1023px" /><p class="wp-caption-text">分页宏示例</p></div>
<h2>
	欢迎贡献代码<br />
</h2>
<p>
	这个项目还刚刚起步，各方面都有需要完善的地方，近期我会为它编写一份完善的文档，欢迎有兴趣的朋友<a href="https://github.com/greyli/bootstrap-flask">贡献代码</a>！</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/integrate-bootstrap-in-flask-application-with-bootstrap-flask/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Flask-Bootstrap：使用本地或其他CDN的资源</title>
		<link>https://greyli.com/flask-bootstrap-use-local-resources-or-other-cdn/</link>
		<comments>https://greyli.com/flask-bootstrap-use-local-resources-or-other-cdn/#comments</comments>
		<pubDate>Tue, 22 Nov 2016 06:11:23 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Bootstrap]]></category>
		<category><![CDATA[Flask]]></category>

		<guid isPermaLink="false">http://withlihui.com/?p=1245</guid>
		<description><![CDATA[Bootstrap的简洁、美观和易用可以让我们在前期不用花费太多的精力和CSS纠缠。 Flask-Bootst [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Bootstrap的简洁、美观和易用可以让我们在前期不用花费太多的精力和CSS纠缠。</p>
<p>Flask-Bootstrap简化了集成Bootstrap的过程，而且提供了一些方便的工具。我们可以在模板里使用它提供的base.html来创建我们自己的基模板。</p>
<p>在我们的基模板开头引入Flask-Bootstrap提供的base.html，这会帮我们加载所有Bootstrap的资源：</p>
<pre lang="text">{% extends "bootstrap/base.html" %}</pre>
<p>除此之外，它提供的wtf.html模板里有帮助我们快速生成Bootstrap样式的表单函数（比如`quick_form()`）。如果你要考虑到IE9的兼容问题，可以引入它提供的fixes.html。</p>
<p>使用Flask-Bootstrap最常见的问题就是它的CDN问题。它默认使用cdnjs.cloudflare.com的Bootstrap资源，而国内访问速度很慢。</p>
<h2><b>加载本地资源</b></h2>
<p>我们可以通过简单的传入一个配置参数来使用本地的Bootstrap资源：</p>
<pre lang="text">app = Flask(__name__)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True</pre>
<h2><b>使用其他CDN</b></h2>
<p>如果你想使用其他CDN资源，那么可以直接在Flask-Bootstrap的源码里修改，找到<b>\venv\Lib\site-packages\flask_bootstrap\__init__.py</b>，在文件末尾，将下面这些文件的地址修改成你想引用的CDN地址即可：</p>
<pre lang="text">bootstrap = lwrap(
     WebCDN('//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/%s/' %
                   BOOTSTRAP_VERSION), local)

jquery = lwrap(
     WebCDN('//cdnjs.cloudflare.com/ajax/libs/jquery/%s/' %
                   JQUERY_VERSION), local)

html5shiv = lwrap(
    WebCDN('//cdnjs.cloudflare.com/ajax/libs/html5shiv/%s/' %
                   HTML5SHIV_VERSION))

respondjs = lwrap(
    WebCDN('//cdnjs.cloudflare.com/ajax/libs/respond.js/%s/' %
                   RESPONDJS_VERSION))
</pre>
<p>比如换成cdn.bootcss.com提供的资源：</p>
<pre lang="text">bootstrap = lwrap(
    WebCDN('//cdn.bootcss.com/bootstrap/%s/' % BOOTSTRAP_VERSION), local)

jquery = lwrap(
    WebCDN('//cdn.bootcss.com/jquery/%s/' % JQUERY_VERSION), local)

html5shiv = lwrap(
    WebCDN('//cdn.bootcss.com/html5shiv/%s/' % HTML5SHIV_VERSION))

respondjs = lwrap(
    WebCDN('//cdn.bootcss.com/respond.js/%s/' % RESPONDJS_VERSION))</pre>
<p>&nbsp;</p>
<h2><b>参考链接</b></h2>
<p>Flask-Bootstrap源码：<a class="" href="https://github.com/mbr/flask-bootstrap" data-editable="true" data-title="GitHub - mbr/flask-bootstrap: Ready-to-use Twitter-bootstrap for use in Flask">https://github.com/mbr/flask-bootstrap</a></p>
<p>Bootstrap源码：<a class="" href="https://github.com/twbs/bootstrap" data-editable="true" data-title="GitHub - twbs/bootstrap: The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.">https://github.com/twbs/bootstrap</a></p>
<p>Flask-Bootstrap文档：<a class="" href="http://pythonhosted.org/Flask-Bootstrap/" data-editable="true" data-title="Flask-Bootstrap">http://pythonhosted.org/Flask-Bootstrap/</a></p>
<p>Flask-Bootstrap中文文档：<a class="" href="http://flask-bootstrap-zh.readthedocs.io/zh/latest/" data-editable="true" data-title="Flask-Bootstrap">http://flask-bootstrap-zh.readthedocs.io/zh/latest/</a></p>
<p>Bootstrap：<a class="" href="http://getbootstrap.com/" data-editable="true" data-title="Bootstrap · The world's most popular mobile-first and responsive front-end framework.">http://getbootstrap.com/</a>（官网） 或 <a class="" href="http://www.bootcss.com/" data-editable="true" data-title="Bootstrap中文网">Bootstrap中文网</a></p>
<p>（Flask-Bootstrap的中文文档是我业余时间翻译的，如果你发现错误，欢迎到Github上提交修改。项目地址： <a class="" href="https://github.com/greyli/flask-bootstrap-docs-zh" data-editable="true" data-title="GitHub - greyli/flask-bootstrap-docs-zh: Flask-Bootstrap中文文档">https://github.com/greyli/flask-bootstrap-docs-zh</a>）</p>
<p>更多关于Flask的优质内容，欢迎关注<a class="" href="https://zhuanlan.zhihu.com/flask" data-editable="true" data-title="Hello, Flask! - 知乎专栏">Hello, Flask! &#8211; 知乎专栏</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/flask-bootstrap-use-local-resources-or-other-cdn/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flask问题集：flash消息分类与美化</title>
		<link>https://greyli.com/flask-set-let-flash-message-supports-a-bootstrap-message-style/</link>
		<comments>https://greyli.com/flask-set-let-flash-message-supports-a-bootstrap-message-style/#respond</comments>
		<pubDate>Wed, 26 Oct 2016 07:16:22 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Bootstrap]]></category>
		<category><![CDATA[Flask]]></category>

		<guid isPermaLink="false">http://withlihui.com/?p=1153</guid>
		<description><![CDATA[&#160; Flask提供了一个很方便的flash函数，在视图函数里只需要一行就可以在网页上闪现一个消息（a [&#8230;]]]></description>
				<content:encoded><![CDATA[<div id="attachment_1155" style="width: 1032px" class="wp-caption aligncenter"><a href="http://greyli.com/wp-content/uploads/2016/10/alert.jpg" rel="attachment wp-att-1155"><img class="size-full wp-image-1155" src="http://greyli.com/wp-content/uploads/2016/10/alert.jpg" alt="bootstrap alert style" width="1022" height="381" srcset="https://greyli.com/wp-content/uploads/2016/10/alert.jpg 1022w, https://greyli.com/wp-content/uploads/2016/10/alert-150x56.jpg 150w, https://greyli.com/wp-content/uploads/2016/10/alert-300x112.jpg 300w, https://greyli.com/wp-content/uploads/2016/10/alert-624x233.jpg 624w" sizes="(max-width: 1022px) 100vw, 1022px" /></a><p class="wp-caption-text">Bootstrap提供的alert样式，依次为：‘success’、‘info’、‘warning’、‘danger’。</p></div>
<p>&nbsp;</p>
<p>Flask提供了一个很方便的flash函数，在视图函数里只需要一行就可以在网页上闪现一个消息（alert），像这样：</p>
<pre lang="python">flash(u'登录成功，欢迎回来！')</pre>
<p>我们今天来为它添加样式。</p>
<p>&nbsp;</p>
<h2><b>基本用法</b></h2>
<p>在视图函数里使用flash：</p>
<pre lang="python">flash(u'要闪现的消息内容')</pre>
<p>然后在基模板里使用`get_flashed_messages()`来获取消息，并渲染出来。<br />比如《Flask Web开发》里使用alert-warning渲染所有的消息：</p>
<pre lang="html">{% for message in get_flashed_messages() %}
&lt;div class="alert alert-warning"&gt;
    &lt;button type="button" class="close" data-dismiss="alert"&gt;&amp;times;&lt;/button&gt;
    {{ message }}
&lt;/div&gt;
{% endfor %}</pre>
<p>下面我们使用Bootsrtap的消息样式来渲染不同的消息。</p>
<p>&nbsp;</p>
<h2><b>使用Bootstrap的消息（alert）样式</b></h2>
<p>如果想要开启消息的分类，需要在调用`get_flashed_messages()`时传入参数`with_categories=True`。这时，在基模板里使用这个函数获取消息时，得到的是一个由`(category, message)`形式的元组组成的一个列表。</p>
<p>之后，就可以在使用flash函数时加入消息类别：</p>
<pre lang="python">flash(u'登录成功，欢迎回来！', 'info')</pre>
<p>你可以使用<strong>‘success’、‘info’、‘warning’、‘danger’</strong>这四个值中的任一个。具体样式文章开头的图片。</p>
<p>这时基模板里渲染消息的部分要改成这样：</p>
<pre lang="html">{% for message in get_flashed_messages(with_categories=True) %}
&lt;div class="alert alert-{{ message[0] }}"&gt;
    &lt;button type="button" class="close" data-dismiss="alert"&gt;&amp;times;&lt;/button&gt;
    {{ message[1] }}
&lt;/div&gt;
{% endfor %}</pre>
<p>这样一来，每个消息就会按照我们提供的分类样式来渲染了。</p>
<p>&nbsp;</p>
<h2><b>DIY</b></h2>
<p>当然，你也可以自己定义每一类消息的样式。我推荐你在Bootstrap的四个类别的基础上增加样式，这样只需要在你的css文件里添加一个新的alert样式，比如下面我添加了一个code样式：</p>
<pre lang="css">/* code alert style, by Grey Li*/
.alert-code {
    color: #66ff66;
    background-color: #000000;
    border-color: #ebccd1;
}

.alert-code hr {
    border-top-color: #000066;
}

.alert-code .alert-link {
    color: #ff9900;
}</pre>
<p>然后使用：</p>
<pre lang="python">flash(u'要闪现的消息', 'code')</pre>
<p>效果：</p>
<div id="attachment_1210" style="width: 552px" class="wp-caption aligncenter"><a href="http://greyli.com/wp-content/uploads/2016/10/alert.png" rel="attachment wp-att-1210"><img class="size-full wp-image-1210" src="http://greyli.com/wp-content/uploads/2016/10/alert.png" alt="我设计的alert样式" width="542" height="83" srcset="https://greyli.com/wp-content/uploads/2016/10/alert.png 542w, https://greyli.com/wp-content/uploads/2016/10/alert-150x23.png 150w, https://greyli.com/wp-content/uploads/2016/10/alert-300x46.png 300w" sizes="(max-width: 542px) 100vw, 542px" /></a><p class="wp-caption-text">我设计的alert样式：p</p></div>
<p>如果你不用Bootstrap，只需要加将上面的为div指定类的那行改成：</p>
<pre lang="html">&lt;div class="{{ message[0] }}"&gt;</pre>
<p>然后在你的css文件里定义类别对应的样式就可以了。</p>
<p>&nbsp;</p>
<h2><b>多看源码</b></h2>
<p>源码里面有很多有趣和有价值的东西，要养成看源码的习惯。<b>很多问题靠看源码里的注释基本上就能解决</b>。所以，问题的正确解决方式应该是：看源码——Google——提问。</p>
<ul>
<li>flash()和get_flashed_messages()的源码：<a class="" href="https://github.com/pallets/flask/blob/master/flask/helpers.py" data-editable="true" data-title="flask/helpers.py at master · pallets/flask · GitHub">https://github.com/pallets/flask/blob/master/flask/helpers.py</a>（从第363行开始）</li>
<li>flask文档（Message Flashing）：<a class="" href="http://flask.pocoo.org/docs/0.11/patterns/flashing/" data-editable="true" data-title="Message Flashing">http://flask.pocoo.org/docs/0.11/patterns/flashing/</a></li>
<li>Bootstrap文档（A）：<a class="" href="http://v4-alpha.getbootstrap.com/components/alerts/" data-editable="true" data-title="Alerts · Bootstrap">Alerts · Bootstrap</a>。</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/flask-set-let-flash-message-supports-a-bootstrap-message-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
