{"id":307003,"date":"2024-05-20T20:24:36","date_gmt":"2024-05-20T12:24:36","guid":{"rendered":""},"modified":"2024-05-20T20:24:55","modified_gmt":"2024-05-20T12:24:55","slug":"pycharm-%e4%b8%ad-flask-%e6%a1%86%e6%9e%b6%e4%b8%8b-bootstrap-%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e5%af%bc%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/307003.html","title":{"rendered":"pycharm \u4e2d flask \u6846\u67b6\u4e0b bootstrap \u5982\u4f55\u8fdb\u884c\u5bfc\u5165"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26223416\/26864e3b-2542-4e86-b60f-b479fc007b35.webp\" alt=\"pycharm \u4e2d flask \u6846\u67b6\u4e0b bootstrap \u5982\u4f55\u8fdb\u884c\u5bfc\u5165\" \/><\/p>\n<p><p>Bootstrap\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u5bfc\u5165\u5230Flask\u9879\u76ee\u4e2d\u3002<strong>\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528CDN\u94fe\u63a5<\/strong>\u3001<strong>\u5c06Bootstrap\u6587\u4ef6\u4e0b\u8f7d\u5230\u672c\u5730\u5e76\u901a\u8fc7\u9759\u6001\u6587\u4ef6\u5939\u5f15\u7528<\/strong>\u3001<strong>\u6216\u4f7f\u7528Flask\u6269\u5c55\u5982Flask-Bootstrap\u8fdb\u884c\u6574\u5408<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u5c06\u4fa7\u91cd\u4e8e\u4f7f\u7528Flask\u6269\u5c55Flask-Bootstrap\uff0c\u5b83\u5c06Bootstrap\u4e0eFlask\u65e0\u7f1d\u96c6\u6210\u3002Flask-Bootstrap\u5c01\u88c5\u4e86Bootstrap\u7684HTML\u548cCSS\u57fa\u7840\uff0c\u4fbf\u4e8e\u5728Flask\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5feb\u901f\u4f7f\u7528Bootstrap\u3002\u8981\u4f7f\u7528\u8be5\u6269\u5c55\uff0c\u60a8\u9700\u8981\u9884\u5148\u5b89\u88c5Flask\u548cFlask-Bootstrap\u3002\u901a\u8fc7pip\u60a8\u53ef\u4ee5\u8f7b\u677e\u5b89\u88c5\u5b83\u4eec\uff0c\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\uff1a<code>pip install flask flask-bootstrap<\/code>\u3002\u5b8c\u6210\u5b89\u88c5\u540e\uff0c\u60a8\u53ef\u4ee5\u5728Flask\u7684\u89c6\u56fe\u51fd\u6570\u4e2d\u4f7f\u7528Bootstrap\u63d0\u4f9b\u7684\u6a21\u677f\u548c\u8868\u5355\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5FLASK-BOOTSTRAP<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u5de5\u4f5c\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Flask\u3002\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528PIP\u5305\u7ba1\u7406\u5668\u5b89\u88c5Flask-Bootstrap\u3002\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u952e\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flask-bootstrap<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5728Flask\u5e94\u7528\u4e2d\u5bfc\u5165Flask-Bootstrap\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6eFLASK APP<\/h3>\n<\/p>\n<p><p>\u5f00\u59cb\u4e4b\u524d\uff0c\u521d\u59cb\u5316Flask\u5e94\u7528\u7a0b\u5e8f\uff0c\u5728\u4f60\u7684<code>app.py<\/code>\u6216<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\u6587\u4ef6\u4e2d\u914d\u7f6eFlask-Bootstrap\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>from flask_bootstrap import Bootstrap<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>Bootstrap(app)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e4b\u540e\uff0cFlask-Bootstrap\u5df2\u7ecf\u96c6\u6210\u5230\u4f60\u7684\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u5728\u4f60\u7684\u6a21\u677f\u91cc\u4f7f\u7528\u5b83\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efaBOOTSTRAP BASE TEMPLATE<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Flask\u5e94\u7528\u4e2d\u4f7f\u7528Bootstrap\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u57fa\u7840\u6a21\u677f\uff0c\u8fd9\u901a\u5e38\u662f\u4e00\u4e2aHTML\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e86Bootstrap\u6846\u67b6\u7684\u94fe\u63a5\u548c\u5fc5\u9700\u7684\u5143\u7d20\u3002\u901a\u8fc7\u6269\u5c55Flask-Bootstrap\u63d0\u4f9b\u7684\u57fa\u7840\u6a21\u677f\uff0c\u53ef\u4ee5\u786e\u4fddBootstrap\u529f\u80fd\u7684\u53ef\u7528\u6027\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>base.html<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    {% block head %}<\/p>\n<p>    &lt;title&gt;{% block title %}{% endblock %} - My Flask App&lt;\/title&gt;<\/p>\n<p>    {% include &#039;bootstrap\/base.html&#039; %}<\/p>\n<p>    {% endblock %}<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;div class=&quot;container&quot;&gt;<\/p>\n<p>        {% block body %}<\/p>\n<p>        {% endblock %}<\/p>\n<p>    &lt;\/div&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528BOOTSTRAP COMPONENTS<\/h3>\n<\/p>\n<p><p>\u73b0\u5728\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u89c6\u56fe\u6a21\u677f\u4e2d\u4f7f\u7528Flask-Bootstrap\u3002\u521b\u5efa\u4e00\u4e2a\u65b0\u7684HTML\u6587\u4ef6\uff0c\u6bd4\u5982<code>index.html<\/code>\uff0c\u7136\u540e\u6269\u5c55<code>base.html<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">{% extends &quot;base.html&quot; %}<\/p>\n<p>{% block title %}Home{% endblock %}<\/p>\n<p>{% block body %}<\/p>\n<p>&lt;h1&gt;Welcome to My Flask App&lt;\/h1&gt;<\/p>\n<p>&lt;p&gt;This is a sample page using Flask and Bootstrap!&lt;\/p&gt;<\/p>\n<p>&lt;!-- Add Bootstrap components here --&gt;<\/p>\n<p>{% endblock %}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u60a8\u7684Flask\u89c6\u56fe\u5c06\u7ee7\u627fBootstrap\u6837\u5f0f\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528\u6240\u6709Bootstrap\u7ec4\u4ef6\uff0c\u5982\u6309\u94ae\u3001\u5bfc\u822a\u680f\u548c\u5361\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001CUSTOMIZING BOOTSTRAP<\/h3>\n<\/p>\n<p><p>\u867d\u7136Flask-Bootstrap\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u5f15\u5165Bootstrap\uff0c\u4f46\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u5bf9\u6837\u5f0f\u8fdb\u884c\u81ea\u5b9a\u4e49\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684CSS\u6587\u4ef6\u6765\u8986\u76d6Bootstrap\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5728\u4f60\u7684Flask\u5e94\u7528\u7684<code>static<\/code>\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684CSS\u6587\u4ef6\uff0c\u6bd4\u5982<code>custom.css<\/code>\uff0c\u5e76\u5728<code>base.html<\/code>\u4e2d\u5f15\u5165\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">{% block head %}<\/p>\n<p>&lt;!-- Existing head contents --&gt;<\/p>\n<p>&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url_for(&#039;static&#039;, filename=&#039;custom.css&#039;) }}&quot;&gt;<\/p>\n<p>{% endblock %}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728<code>custom.css<\/code>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u81ea\u5df1\u7684\u6837\u5f0f\uff0c\u8fd9\u4e9b\u6837\u5f0f\u5c06\u6539\u53d8Bootstrap\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001CONCLUSION<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u5df2\u7ecf\u6210\u529f\u5730\u5728Flask\u5e94\u7528\u4e2d\u5bfc\u5165\u4e86Bootstrap\uff0c\u8fd9\u5c06\u4f7f\u4f60\u7684\u7f51\u7ad9\u80fd\u591f\u5feb\u901f\u800c\u4e14\u65e0\u7f1d\u5730\u54cd\u5e94\u4e0d\u540c\u8bbe\u5907\u548c\u5c4f\u5e55\u5c3a\u5bf8\u3002\u4f60\u53ef\u4ee5\u5229\u7528Bootstrap\u5e9e\u5927\u7684\u7ec4\u4ef6\u5e93\u6765\u52a0\u901f\u5f00\u53d1\u8fc7\u7a0b\uff0c\u5e76\u4fdd\u6301\u4e00\u4e2a\u73b0\u4ee3\u548c\u54cd\u5e94\u5f0f\u7684\u754c\u9762\u8bbe\u8ba1\u3002Bootstrap\u548cFlask\u7684\u7ed3\u5408\u6253\u5f00\u4e86\u4e00\u4e2a\u5feb\u901f\u3001\u9ad8\u6548\u3001\u7f8e\u89c2\u7684web\u5e94\u7528\u5f00\u53d1\u4e4b\u8def\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728PyCharm\u4e2d\u5bfc\u5165Flask\u6846\u67b6\u4e0b\u7684Bootstrap\uff1f<\/strong><\/p>\n<ol>\n<li><strong>Flask\u6846\u67b6\u4e2d\u5982\u4f55\u4f7f\u7528Bootstrap\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u5728Flask\u6846\u67b6\u4e2d\u4f7f\u7528Bootstrap\u53ef\u4ee5\u4e3a\u4f60\u7684Web\u5e94\u7528\u6dfb\u52a0\u7f8e\u89c2\u7684\u754c\u9762\u548c\u4ea4\u4e92\u6548\u679c\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5728\u9879\u76ee\u4e2d\u5b89\u88c5\u4e86Bootstrap\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728\u7ec8\u7aef\u4e2d\u5b89\u88c5Bootstrap\uff1a<\/p>\n<pre><code>pip install flask-bootstrap\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684Flask\u5e94\u7528\u4e2d\u5bfc\u5165Bootstrap\u6a21\u5757\uff1a<\/p>\n<pre><code class=\"language-python\">from flask_bootstrap import Bootstrap\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u521b\u5efaBootstrap\u5bf9\u8c61\u5e76\u5c06\u5e94\u7528\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\uff1a<\/p>\n<pre><code class=\"language-python\">app = Flask(__name__)\nbootstrap = Bootstrap(app)\n<\/code><\/pre>\n<p>\u73b0\u5728\u4f60\u5c31\u53ef\u4ee5\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\u4f7f\u7528Bootstrap\u4e86\u3002<\/p>\n<ol start=\"2\">\n<li><strong>\u5982\u4f55\u5bfc\u5165Bootstrap\u6a21\u677f\u5230Flask\u9879\u76ee\u4e2d\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u4e3a\u4e86\u5728Flask\u9879\u76ee\u4e2d\u4f7f\u7528Bootstrap\u6a21\u677f\uff0c\u9996\u5148\u9700\u8981\u5c06Bootstrap\u7684CSS\u548cJS\u6587\u4ef6\u653e\u5165\u9879\u76ee\u7684static\u6587\u4ef6\u5939\u4e2d\u3002\u4f60\u53ef\u4ee5\u4ece\u5b98\u65b9\u7f51\u7ad9\u4e0a\u4e0b\u8f7dBootstrap\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u89e3\u538b\u5230\u9879\u76ee\u7684static\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684Flask\u5e94\u7528\u4e2d\u521b\u5efa\u4e00\u4e2a\u57fa\u7840\u6a21\u677fbase.html\uff0c\u5e76\u5728\u5176\u4e2d\u5bfc\u5165Bootstrap\u7684CSS\u548cJS\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    {% block head %}\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url_for(&#039;static&#039;, filename=&#039;css\/bootstrap.min.css&#039;) }}&quot;&gt;\n    {% endblock %}\n&lt;\/head&gt;\n&lt;body&gt;\n    {% block content %}\n    &lt;!-- \u9875\u9762\u5185\u5bb9 --&gt;\n    {% endblock %}\n\n    {% block scripts %}\n    &lt;script src=&quot;{{ url_for(&#039;static&#039;, filename=&#039;js\/bootstrap.min.js&#039;) }}&quot;&gt;&lt;\/script&gt;\n    {% endblock %}\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5728\u5176\u4ed6\u6a21\u677f\u4e2d\u7ee7\u627f\u57fa\u7840\u6a21\u677fbase.html\uff0c\u5e76\u5728{% block content %}\u4e2d\u7f16\u5199\u5177\u4f53\u7684\u9875\u9762\u5185\u5bb9\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u6bcf\u4e2a\u9875\u9762\u4e2d\u90fd\u80fd\u4f7f\u7528Bootstrap\u7684\u6837\u5f0f\u548c\u7ec4\u4ef6\u3002<\/p>\n<ol start=\"3\">\n<li><strong>\u5982\u4f55\u4f7f\u7528Bootstrap\u7684\u7ec4\u4ef6\u548c\u6837\u5f0f\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u4e00\u65e6\u5728Flask\u4e2d\u5bfc\u5165\u4e86Bootstrap\u548c\u8bbe\u7f6e\u4e86\u6a21\u677f\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\u4f7f\u7528Bootstrap\u7684\u5404\u79cd\u7ec4\u4ef6\u548c\u6837\u5f0f\u4e86\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5728\u9875\u9762\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\uff1a<\/p>\n<pre><code class=\"language-html\">{% extends &#039;base.html&#039; %}\n\n{% block content %}\n    &lt;button class=&quot;btn btn-primary&quot;&gt;\u70b9\u51fb\u6211&lt;\/button&gt;\n{% endblock %} \n<\/code><\/pre>\n<p>\u901a\u8fc7\u9605\u8bfbBootstrap\u7684\u6587\u6863\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4e86\u89e3\u5230\u66f4\u591a\u7684\u7ec4\u4ef6\u548c\u6837\u5f0f\uff0c\u5e76\u6839\u636e\u9700\u6c42\u4f7f\u7528\u5b83\u4eec\u6765\u6784\u5efa\u4f60\u7684Web\u5e94\u7528\u754c\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Bootstrap\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u5bfc\u5165\u5230Flask\u9879\u76ee\u4e2d\u3002\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528CDN\u94fe\u63a5\u3001\u5c06Bootstra [&hellip;]","protected":false},"author":3,"featured_media":307022,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/307003"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=307003"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/307003\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/307022"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=307003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=307003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=307003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}