{"id":1179812,"date":"2025-01-15T18:29:02","date_gmt":"2025-01-15T10:29:02","guid":{"rendered":""},"modified":"2025-01-15T18:29:12","modified_gmt":"2025-01-15T10:29:12","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8html%e4%b8%adfor","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179812.html","title":{"rendered":"python\u5982\u4f55\u5728html\u4e2dfor"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114129\/07fccce1-a458-4520-881e-3e816d2d72f2.webp\" alt=\"python\u5982\u4f55\u5728html\u4e2dfor\" \/><\/p>\n<p><p> \u5728HTML\u4e2d\u4f7f\u7528Python\u8fdb\u884cfor\u5faa\u73af\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u677f\u5f15\u64ce\u5982Jinja2\u6765\u5b9e\u73b0\u3002Jinja2\u662f\u4e00\u79cd\u7528\u4e8ePython\u7684\u6a21\u677f\u5f15\u64ce\uff0c\u80fd\u591f\u5c06Python\u4ee3\u7801\u5d4c\u5165HTML\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u52a8\u6001\u7f51\u9875\u751f\u6210\u3002<strong>Jinja2\u6a21\u677f\u5f15\u64ce\u3001\u5d4c\u5165\u5faa\u73af\u8bed\u53e5\u3001\u53d8\u91cf\u8f93\u51fa<\/strong>\uff0c\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7Jinja2\u5728HTML\u4e2d\u4f7f\u7528for\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Jinja2\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Jinja2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from jinja2 import Environment, FileSystemLoader<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aJinja2\u73af\u5883\u5b9e\u4f8b<\/strong><\/h2>\n<p>env = Environment(loader=FileSystemLoader(&#39;templates&#39;))<\/p>\n<h2><strong>\u52a0\u8f7d\u6a21\u677f\u6587\u4ef6<\/strong><\/h2>\n<p>template = env.get_template(&#39;index.html&#39;)<\/p>\n<h2><strong>\u6a21\u62df\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;title&#39;: &#39;My Web Page&#39;,<\/p>\n<p>    &#39;items&#39;: [&#39;Item 1&#39;, &#39;Item 2&#39;, &#39;Item 3&#39;, &#39;Item 4&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u6e32\u67d3\u6a21\u677f\u5e76\u751f\u6210HTML<\/strong><\/h2>\n<p>output = template.render(data=data)<\/p>\n<h2><strong>\u5c06\u6e32\u67d3\u7ed3\u679c\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.html&#39;, &#39;w&#39;) as f:<\/p>\n<p>    f.write(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>jinja2<\/code>\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2aJinja2\u73af\u5883\u5b9e\u4f8b\uff0c\u5e76\u6307\u5b9a\u6a21\u677f\u6587\u4ef6\u7684\u52a0\u8f7d\u8def\u5f84\u3002\u63a5\u7740\uff0c\u52a0\u8f7d\u540d\u4e3a<code>index.html<\/code>\u7684\u6a21\u677f\u6587\u4ef6\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u52a8\u6001\u6570\u636e\u7684\u5b57\u5178\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>render<\/code>\u65b9\u6cd5\u6e32\u67d3\u6a21\u677f\uff0c\u5e76\u5c06\u751f\u6210\u7684HTML\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>index.html<\/code>\u7684\u6a21\u677f\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u4f7f\u7528Jinja2\u7684\u8bed\u6cd5\u7f16\u5199HTML\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;{{ data.title }}&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;{{ data.title }}&lt;\/h1&gt;<\/p>\n<p>    &lt;ul&gt;<\/p>\n<p>        {% for item in data.items %}<\/p>\n<p>            &lt;li&gt;{{ item }}&lt;\/li&gt;<\/p>\n<p>        {% endfor %}<\/p>\n<p>    &lt;\/ul&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6a21\u677f\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Jinja2\u7684\u6a21\u677f\u8bed\u6cd5\u3002<code>{{ data.title }}<\/code>\u7528\u4e8e\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff0c\u800c<code>{% for item in data.items %}<\/code>\u5219\u7528\u4e8e\u904d\u5386\u5217\u8868\u5e76\u751f\u6210HTML\u4ee3\u7801\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728HTML\u4e2d\u4f7f\u7528Python\u7684for\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001JINJA2\u6a21\u677f\u5f15\u64ce<\/h3>\n<\/p>\n<p><p>Jinja2 \u662f\u4e00\u4e2a\u73b0\u4ee3\u7684\uff0c\u8bbe\u8ba1\u4f18\u96c5\u7684 Python \u6a21\u677f\u5f15\u64ce\u3002\u5b83\u4e0d\u4ec5\u652f\u6301\u57fa\u672c\u7684\u53d8\u91cf\u66ff\u6362\u548c\u8868\u8fbe\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u63a7\u5236\u6d41\u8bed\u53e5\u548c\u8fc7\u6ee4\u5668\u6765\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u529f\u80fd\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u53d8\u91cf\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u5728Jinja2\u4e2d\uff0c\u53d8\u91cf\u66ff\u6362\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u8981\u5728HTML\u4e2d\u4f7f\u7528\u53cc\u5927\u62ec\u53f7\u5305\u88f9\u53d8\u91cf\u540d\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;p&gt;Hello, {{ name }}!&lt;\/p&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u4f1a\u5c06<code>name<\/code>\u53d8\u91cf\u7684\u503c\u63d2\u5165\u5230HTML\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8868\u8fbe\u5f0f\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>Jinja2\u652f\u6301\u5728\u6a21\u677f\u4e2d\u8fdb\u884c\u8868\u8fbe\u5f0f\u8ba1\u7b97\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;p&gt;The result is {{ 3 + 4 }}.&lt;\/p&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u4f1a\u8ba1\u7b973+4\u7684\u7ed3\u679c\uff0c\u5e76\u5c06\u5176\u63d2\u5165\u5230HTML\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u63a7\u5236\u6d41\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>Jinja2\u652f\u6301\u591a\u79cd\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u5305\u62ecif\u8bed\u53e5\u548cfor\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">{% if user.is_authenticated %}<\/p>\n<p>    &lt;p&gt;Welcome, {{ user.username }}!&lt;\/p&gt;<\/p>\n<p>{% else %}<\/p>\n<p>    &lt;p&gt;Please log in.&lt;\/p&gt;<\/p>\n<p>{% endif %}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;ul&gt;<\/p>\n<p>    {% for item in items %}<\/p>\n<p>        &lt;li&gt;{{ item }}&lt;\/li&gt;<\/p>\n<p>    {% endfor %}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5d4c\u5165\u5faa\u73af\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5728Jinja2\u4e2d\u5d4c\u5165\u5faa\u73af\u8bed\u53e5\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>{% for ... %}<\/code>\u8bed\u53e5\u6765\u904d\u5386\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u904d\u5386\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9879\u76ee\u7684\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Jinja2\u7684for\u5faa\u73af\u8bed\u53e5\u6765\u904d\u5386\u5e76\u751f\u6210HTML\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;ul&gt;<\/p>\n<p>    {% for item in items %}<\/p>\n<p>        &lt;li&gt;{{ item }}&lt;\/li&gt;<\/p>\n<p>    {% endfor %}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u904d\u5386<code>items<\/code>\u5217\u8868\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u9879\u76ee\u751f\u6210\u4e00\u4e2a<code>&lt;li&gt;<\/code>\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5d4c\u5957\u5faa\u73af<\/h4>\n<\/p>\n<p><p>Jinja2\u652f\u6301\u5d4c\u5957\u5faa\u73af\uff0c\u8fd9\u5bf9\u4e8e\u751f\u6210\u590d\u6742\u7684HTML\u7ed3\u6784\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;table&gt;<\/p>\n<p>    {% for row in table %}<\/p>\n<p>        &lt;tr&gt;<\/p>\n<p>            {% for cell in row %}<\/p>\n<p>                &lt;td&gt;{{ cell }}&lt;\/td&gt;<\/p>\n<p>            {% endfor %}<\/p>\n<p>        &lt;\/tr&gt;<\/p>\n<p>    {% endfor %}<\/p>\n<p>&lt;\/table&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u904d\u5386\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u5e76\u751f\u6210\u4e00\u4e2aHTML\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5faa\u73af\u63a7\u5236<\/h4>\n<\/p>\n<p><p>Jinja2\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7279\u6b8a\u7684\u53d8\u91cf\u548c\u8bed\u53e5\u6765\u63a7\u5236\u5faa\u73af\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>loop.index<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u5faa\u73af\u7684\u7d22\u5f15\uff0c\u6216\u8005\u4f7f\u7528<code>continue<\/code>\u548c<code>break<\/code>\u8bed\u53e5\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;ul&gt;<\/p>\n<p>    {% for item in items %}<\/p>\n<p>        {% if loop.index == 3 %}<\/p>\n<p>            &lt;li&gt;Special Item&lt;\/li&gt;<\/p>\n<p>            {% continue %}<\/p>\n<p>        {% endif %}<\/p>\n<p>        &lt;li&gt;{{ item }}&lt;\/li&gt;<\/p>\n<p>    {% endfor %}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u53d8\u91cf\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728Jinja2\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cc\u5927\u62ec\u53f7<code>{{ }}<\/code>\u6765\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;p&gt;{{ variable }}&lt;\/p&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u4f1a\u5c06<code>variable<\/code>\u7684\u503c\u63d2\u5165\u5230HTML\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8f93\u51fa\u7b80\u5355\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u7528\u6237\u540d\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Jinja2\u6765\u8f93\u51fa\u5b83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;p&gt;Welcome, {{ username }}!&lt;\/p&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06<code>username<\/code>\u7684\u503c\u63d2\u5165\u5230HTML\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8f93\u51fa\u5b57\u5178\u4e2d\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Jinja2\u6765\u8f93\u51fa\u5176\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;p&gt;Name: {{ user.name }}&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;Em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l: {{ user.email }}&lt;\/p&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06<code>user<\/code>\u5b57\u5178\u4e2d\u7684<code>name<\/code>\u548c<code>email<\/code>\u503c\u63d2\u5165\u5230HTML\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8fc7\u6ee4\u5668<\/h4>\n<\/p>\n<p><p>Jinja2\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u8fc7\u6ee4\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u548c\u8f6c\u6362\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;p&gt;Uppercase: {{ text | upper }}&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;Default: {{ value | default(&#39;N\/A&#39;) }}&lt;\/p&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06<code>text<\/code>\u53d8\u91cf\u7684\u503c\u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u5e76\u5728<code>value<\/code>\u53d8\u91cf\u4e3a\u7a7a\u65f6\u663e\u793a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001JINJA2\u7684\u9ad8\u7ea7\u7279\u6027<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u6a21\u677f\u529f\u80fd\u5916\uff0cJinja2\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u7075\u6d3b\u5730\u751f\u6210HTML\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b8f<\/h4>\n<\/p>\n<p><p>Jinja2\u7684\u5b8f\u529f\u80fd\u7c7b\u4f3c\u4e8e\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u91cd\u590d\u7684\u6a21\u677f\u4ee3\u7801\u5c01\u88c5\u8d77\u6765\uff0c\u5e76\u5728\u591a\u4e2a\u5730\u65b9\u8c03\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">{% macro render_item(item) %}<\/p>\n<p>    &lt;li&gt;{{ item }}&lt;\/li&gt;<\/p>\n<p>{% endmacro %}<\/p>\n<p>&lt;ul&gt;<\/p>\n<p>    {% for item in items %}<\/p>\n<p>        {{ render_item(item) }}<\/p>\n<p>    {% endfor %}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>render_item<\/code>\u7684\u5b8f\uff0c\u5e76\u5728for\u5faa\u73af\u4e2d\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ee7\u627f<\/h4>\n<\/p>\n<p><p>Jinja2\u652f\u6301\u6a21\u677f\u7ee7\u627f\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u57fa\u7840\u6a21\u677f\u6765\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!-- base.html --&gt;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;{% block title %}My Web Page{% endblock %}&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;header&gt;<\/p>\n<p>        &lt;h1&gt;Header&lt;\/h1&gt;<\/p>\n<p>    &lt;\/header&gt;<\/p>\n<p>    &lt;main&gt;<\/p>\n<p>        {% block content %}{% endblock %}<\/p>\n<p>    &lt;\/main&gt;<\/p>\n<p>    &lt;footer&gt;<\/p>\n<p>        &lt;p&gt;Footer&lt;\/p&gt;<\/p>\n<p>    &lt;\/footer&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!-- index.html --&gt;<\/p>\n<p>{% extends &#39;base.html&#39; %}<\/p>\n<p>{% block title %}Home Page{% endblock %}<\/p>\n<p>{% block content %}<\/p>\n<p>    &lt;p&gt;Welcome to the home page!&lt;\/p&gt;<\/p>\n<p>{% endblock %}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u57fa\u7840\u6a21\u677f<code>base.html<\/code>\uff0c\u5e76\u5728\u5b50\u6a21\u677f<code>index.html<\/code>\u4e2d\u7ee7\u627f\u5b83\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5305\u542b<\/h4>\n<\/p>\n<p><p>Jinja2\u7684\u5305\u542b\u529f\u80fd\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\u5d4c\u5165\u5230\u53e6\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!-- header.html --&gt;<\/p>\n<p>&lt;header&gt;<\/p>\n<p>    &lt;h1&gt;Header&lt;\/h1&gt;<\/p>\n<p>&lt;\/header&gt;<\/p>\n<p>&lt;!-- index.html --&gt;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;My Web Page&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    {% include &#39;header.html&#39; %}<\/p>\n<p>    &lt;main&gt;<\/p>\n<p>        &lt;p&gt;Welcome to the home page!&lt;\/p&gt;<\/p>\n<p>    &lt;\/main&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06<code>header.html<\/code>\u7684\u5185\u5bb9\u5d4c\u5165\u5230<code>index.html<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3Jinja2\u7684\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\u6765\u5c55\u793a\u5982\u4f55\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u4f7f\u7528Jinja2\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u535a\u5ba2\u7f51\u7ad9\uff0c\u6211\u4eec\u9700\u8981\u52a8\u6001\u751f\u6210\u535a\u5ba2\u6587\u7ae0\u5217\u8868\u548c\u6587\u7ae0\u8be6\u60c5\u9875\u9762\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9879\u76ee\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>blog\/<\/p>\n<p>    templates\/<\/p>\n<p>        base.html<\/p>\n<p>        index.html<\/p>\n<p>        post.html<\/p>\n<p>    app.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u57fa\u7840\u6a21\u677f<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u57fa\u7840\u6a21\u677f<code>base.html<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;{% block title %}My Blog{% endblock %}&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;header&gt;<\/p>\n<p>        &lt;h1&gt;My Blog&lt;\/h1&gt;<\/p>\n<p>    &lt;\/header&gt;<\/p>\n<p>    &lt;nav&gt;<\/p>\n<p>        &lt;a href=&quot;\/&quot;&gt;Home&lt;\/a&gt;<\/p>\n<p>    &lt;\/nav&gt;<\/p>\n<p>    &lt;main&gt;<\/p>\n<p>        {% block content %}{% endblock %}<\/p>\n<p>    &lt;\/main&gt;<\/p>\n<p>    &lt;footer&gt;<\/p>\n<p>        &lt;p&gt;&amp;copy; 2023 My Blog&lt;\/p&gt;<\/p>\n<p>    &lt;\/footer&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u9996\u9875\u6a21\u677f<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u9996\u9875\u6a21\u677f<code>index.html<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">{% extends &#39;base.html&#39; %}<\/p>\n<p>{% block title %}Home{% endblock %}<\/p>\n<p>{% block content %}<\/p>\n<p>    &lt;h2&gt;Blog Posts&lt;\/h2&gt;<\/p>\n<p>    &lt;ul&gt;<\/p>\n<p>        {% for post in posts %}<\/p>\n<p>            &lt;li&gt;&lt;a href=&quot;\/post\/{{ post.id }}&quot;&gt;{{ post.title }}&lt;\/a&gt;&lt;\/li&gt;<\/p>\n<p>        {% endfor %}<\/p>\n<p>    &lt;\/ul&gt;<\/p>\n<p>{% endblock %}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u6587\u7ae0\u8be6\u60c5\u6a21\u677f<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u6587\u7ae0\u8be6\u60c5\u6a21\u677f<code>post.html<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">{% extends &#39;base.html&#39; %}<\/p>\n<p>{% block title %}{{ post.title }}{% endblock %}<\/p>\n<p>{% block content %}<\/p>\n<p>    &lt;h2&gt;{{ post.title }}&lt;\/h2&gt;<\/p>\n<p>    &lt;p&gt;{{ post.content }}&lt;\/p&gt;<\/p>\n<p>{% endblock %}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001Python\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199Python\u4ee3\u7801\u6765\u6e32\u67d3\u6a21\u677f\u5e76\u751f\u6210HTML\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from jinja2 import Environment, FileSystemLoader<\/p>\n<p>from flask import Flask, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>env = Environment(loader=FileSystemLoader(&#39;templates&#39;))<\/p>\n<h2><strong>\u6a21\u62df\u6570\u636e<\/strong><\/h2>\n<p>posts = [<\/p>\n<p>    {&#39;id&#39;: 1, &#39;title&#39;: &#39;First Post&#39;, &#39;content&#39;: &#39;This is the first post.&#39;},<\/p>\n<p>    {&#39;id&#39;: 2, &#39;title&#39;: &#39;Second Post&#39;, &#39;content&#39;: &#39;This is the second post.&#39;},<\/p>\n<p>    {&#39;id&#39;: 3, &#39;title&#39;: &#39;Third Post&#39;, &#39;content&#39;: &#39;This is the third post.&#39;},<\/p>\n<p>]<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    return render_template(&#39;index.html&#39;, posts=posts)<\/p>\n<p>@app.route(&#39;\/post\/&lt;int:post_id&gt;&#39;)<\/p>\n<p>def post(post_id):<\/p>\n<p>    post = next((p for p in posts if p[&#39;id&#39;] == post_id), None)<\/p>\n<p>    return render_template(&#39;post.html&#39;, post=post)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u6765\u52a8\u6001\u751f\u6210HTML\u9875\u9762\u3002\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u57fa\u7840\u6a21\u677f\uff0c\u7136\u540e\u521b\u5efa\u4e86\u9996\u9875\u6a21\u677f\u548c\u6587\u7ae0\u8be6\u60c5\u6a21\u677f\uff0c\u6700\u540e\u4f7f\u7528Python\u4ee3\u7801\u6e32\u67d3\u6a21\u677f\u5e76\u751f\u6210HTML\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728HTML\u4e2d\u4f7f\u7528Python\u7684for\u5faa\u73af\uff0c\u5e76\u751f\u6210\u52a8\u6001\u7f51\u9875\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cJinja2\u6a21\u677f\u5f15\u64ce\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728HTML\u4e2d\u4f7f\u7528Python\u8fdb\u884cfor\u5faa\u73af\u7b49\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528Jinja2\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210\u52a8\u6001\u7f51\u9875\uff0c\u5e76\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u548c\u6a21\u677f\u7ee7\u627f\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728HTML\u4e2d\u4f7f\u7528Python\u8fdb\u884c\u5faa\u73af\uff1f<\/strong><br \/>\u5728HTML\u4e2d\u76f4\u63a5\u4f7f\u7528Python\u8fdb\u884c\u5faa\u73af\u5e76\u4e0d\u53ef\u884c\uff0c\u56e0\u4e3aHTML\u672c\u8eab\u662f\u9759\u6001\u7684\u6807\u8bb0\u8bed\u8a00\uff0c\u800cPython\u662f\u4e00\u79cd\u52a8\u6001\u7f16\u7a0b\u8bed\u8a00\u3002\u7136\u800c\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6846\u67b6\u548c\u5de5\u5177\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Flask\u6216Django\u7b49Web\u6846\u67b6\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u751f\u6210\u52a8\u6001HTML\u5185\u5bb9\u3002\u901a\u8fc7\u5728\u6a21\u677f\u4e2d\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u7684\u6570\u636e\u4f20\u9012\u5230HTML\u4e2d\u8fdb\u884c\u663e\u793a\u3002<\/p>\n<p><strong>\u4f7f\u7528Flask\u65f6\uff0c\u5982\u4f55\u5728HTML\u6a21\u677f\u4e2d\u8fed\u4ee3\u5217\u8868\uff1f<\/strong><br \/>\u5728Flask\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u6765\u5904\u7406HTML\u3002\u5047\u8bbe\u60a8\u6709\u4e00\u4e2aPython\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u5728HTML\u6a21\u677f\u4e2d\u4f7f\u7528<code>{% for item in list %}<\/code>\u6765\u8fed\u4ee3\u8be5\u5217\u8868\u3002\u6bcf\u6b21\u5faa\u73af\uff0c\u60a8\u53ef\u4ee5\u8bbf\u95ee\u5f53\u524d\u9879\u5e76\u5728HTML\u4e2d\u663e\u793a\u5b83\u3002\u786e\u4fdd\u5728Python\u89c6\u56fe\u51fd\u6570\u4e2d\u5c06\u5217\u8868\u4f5c\u4e3a\u4e0a\u4e0b\u6587\u4f20\u9012\u5230\u6a21\u677f\u4e2d\u3002<\/p>\n<p><strong>Django\u4e2d\u7684\u6a21\u677f\u5faa\u73af\u5982\u4f55\u5de5\u4f5c\uff1f<\/strong><br \/>\u5728Django\u4e2d\uff0c\u6a21\u677f\u5faa\u73af\u4e5f\u91c7\u7528\u7c7b\u4f3c\u7684\u65b9\u5f0f\u3002\u60a8\u53ef\u4ee5\u5728HTML\u6a21\u677f\u4e2d\u4f7f\u7528<code>{% for item in items %}<\/code>\u8bed\u6cd5\u6765\u904d\u5386\u4f20\u9012\u7ed9\u6a21\u677f\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u3002\u5728Django\u89c6\u56fe\u4e2d\uff0c\u60a8\u9700\u8981\u5c06\u6570\u636e\u4f20\u9012\u5230\u6e32\u67d3\u6a21\u677f\u7684\u4e0a\u4e0b\u6587\u5b57\u5178\u4e2d\uff0c\u4ece\u800c\u5728\u6a21\u677f\u4e2d\u8bbf\u95ee\u548c\u5c55\u793a\u5b83\u4eec\u3002\u8fd9\u4f7f\u5f97\u521b\u5efa\u52a8\u6001\u9875\u9762\u53d8\u5f97\u7b80\u5355\u800c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728HTML\u4e2d\u4f7f\u7528Python\u8fdb\u884cfor\u5faa\u73af\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u677f\u5f15\u64ce\u5982Jinja2\u6765\u5b9e\u73b0\u3002Jinja2\u662f\u4e00\u79cd\u7528\u4e8ePyt [&hellip;]","protected":false},"author":3,"featured_media":1179828,"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\/1179812"}],"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=1179812"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179812\/revisions"}],"predecessor-version":[{"id":1179829,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179812\/revisions\/1179829"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179828"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}