<?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-Flask &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/tag/bootstrap-flask/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-Flask &#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>
	</channel>
</rss>
