{"id":1005288,"date":"2024-12-27T10:34:23","date_gmt":"2024-12-27T02:34:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1005288.html"},"modified":"2024-12-27T10:34:25","modified_gmt":"2024-12-27T02:34:25","slug":"html%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%bapython%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1005288.html","title":{"rendered":"html\u5982\u4f55\u663e\u793apython\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081859\/9d8add81-7740-4915-ad40-608efb456fd1.webp\" alt=\"html\u5982\u4f55\u663e\u793apython\u6570\u636e\" \/><\/p>\n<p><p> <strong>HTML\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u663e\u793aPython\u6570\u636e\uff1a\u4f7f\u7528Flask\/Django\u7b49Web\u6846\u67b6\u3001\u901a\u8fc7AJAX\u4e0e\u540e\u7aef\u901a\u4fe1\u3001\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u9002\u5408\u9879\u76ee\u9700\u6c42\u7684\u65b9\u5f0f\u5f88\u91cd\u8981\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528Flask\u6216Django\u7b49Web\u6846\u67b6<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u5f3a\u5927\u7684\u65b9\u5f0f\uff0c\u5b83\u4eec\u53ef\u4ee5\u76f4\u63a5\u5c06Python\u6570\u636e\u6e32\u67d3\u5230HTML\u4e2d\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u590d\u6742\u903b\u8f91\u548c\u5927\u91cf\u6570\u636e\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Flask\u6846\u67b6\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1aFlask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\u3002\u901a\u8fc7Flask\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u5668\u7aef\u5e94\u7528\u7a0b\u5e8f\uff0c\u5c06Python\u6570\u636e\u53d1\u9001\u5230HTML\u6a21\u677f\u4e2d\u8fdb\u884c\u6e32\u67d3\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Flask\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aFlask\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u4ecePython\u4ee3\u7801\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u4f20\u9012\u7ed9HTML\u6a21\u677f\u3002\u901a\u8fc7\u4f7f\u7528Flask\u7684<code>render_template<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u6e32\u67d3\u5230HTML\u6a21\u677f\u4e2d\u3002\u6a21\u677f\u6587\u4ef6\u901a\u5e38\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\uff0c\u8fd9\u5141\u8bb8\u4f60\u5728HTML\u4e2d\u5d4c\u5165Python\u4ee3\u7801\u548c\u6570\u636e\uff0c\u4ee5\u4fbf\u52a8\u6001\u751f\u6210\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001FLASK\/DJANGO\u7b49WEB\u6846\u67b6<\/p>\n<\/p>\n<p><p>Flask\u548cDjango\u662f\u4e24\u4e2a\u6d41\u884c\u7684Python Web\u6846\u67b6\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5c06Python\u6570\u636e\u6e32\u67d3\u5230HTML\u4e2d\u3002\u4f7f\u7528\u8fd9\u4e9b\u6846\u67b6\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u52a8\u6001\u7f51\u9875\u548c\u5904\u7406\u7528\u6237\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Flask\u6846\u67b6<\/strong><\/p>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u4e8e\u4e2d\u5c0f\u578b\u9879\u76ee\u3002\u5176\u7b80\u6d01\u7684\u8bbe\u8ba1\u548c\u6269\u5c55\u6027\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u6839\u636e\u9879\u76ee\u9700\u6c42\u7075\u6d3b\u9009\u62e9\u7ec4\u4ef6\u3002Flask\u7684\u6838\u5fc3\u7406\u5ff5\u662f\u201c\u5fae\u6846\u67b6\u201d\uff0c\u5373\u53ea\u63d0\u4f9b\u6700\u5c0f\u7684\u6838\u5fc3\u529f\u80fd\uff0c\u800c\u5c06\u5176\u4ed6\u529f\u80fd\u901a\u8fc7\u6269\u5c55\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u5728Flask\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u5c06Python\u6570\u636e\u6e32\u67d3\u5230HTML\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Flask\u5c06Python\u6570\u636e\u4f20\u9012\u7ed9HTML\u6a21\u677f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}<\/p>\n<p>    return render_template(&#39;index.html&#39;, data=data)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aFlask\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8def\u7531<code>\/<\/code>\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u6e32\u67d3\u540e\u7684HTML\u6a21\u677f<code>index.html<\/code>\uff0c\u5e76\u5c06Python\u6570\u636e<code>data<\/code>\u4f20\u9012\u7ed9\u6a21\u677f\u3002\u5728\u6a21\u677f\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7Jinja2\u8bed\u6cd5\u8bbf\u95ee\u548c\u663e\u793a\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Django\u6846\u67b6<\/strong><\/p>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u4e8e\u5927\u578b\u9879\u76ee\u548c\u590d\u6742\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u529f\u80fd\uff0c\u5982ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u3001\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u3001\u7ba1\u7406\u754c\u9762\u7b49\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u6784\u5efa\u548c\u90e8\u7f72Web\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Django\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u89c6\u56fe\u51fd\u6570\u5c06Python\u6570\u636e\u4f20\u9012\u7ed9\u6a21\u677f\uff0c\u5e76\u5728\u6a21\u677f\u4e2d\u4f7f\u7528Django\u6a21\u677f\u8bed\u8a00\u8fdb\u884c\u6e32\u67d3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Django\u4e2d\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.shortcuts import render<\/p>\n<p>def index(request):<\/p>\n<p>    data = {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 25}<\/p>\n<p>    return render(request, &#39;index.html&#39;, {&#39;data&#39;: data})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aDjango\u89c6\u56fe\u51fd\u6570<code>index<\/code>\uff0c\u5b83\u5c06Python\u6570\u636e<code>data<\/code>\u4f20\u9012\u7ed9\u6a21\u677f<code>index.html<\/code>\u3002\u5728\u6a21\u677f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Django\u6a21\u677f\u8bed\u8a00\u8bbf\u95ee\u548c\u663e\u793a\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u901a\u8fc7AJAX\u4e0e\u540e\u7aef\u901a\u4fe1<\/p>\n<\/p>\n<p><p>AJAX\uff08Asynchronous JavaScript and XML\uff09\u662f\u4e00\u79cd\u5728\u7f51\u9875\u65e0\u9700\u91cd\u65b0\u52a0\u8f7d\u7684\u60c5\u51b5\u4e0b\u4e0e\u670d\u52a1\u5668\u4ea4\u6362\u6570\u636e\u7684\u6280\u672f\u3002\u901a\u8fc7AJAX\uff0c\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u66f4\u65b0\u7f51\u9875\u5185\u5bb9\u7684\u529f\u80fd\uff0c\u800c\u65e0\u9700\u5237\u65b0\u6574\u4e2a\u9875\u9762\u3002\u8fd9\u5bf9\u4e8e\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u548c\u6027\u80fd\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528AJAX\u83b7\u53d6Python\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u524d\u7aef\u9875\u9762\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684AJAX\u8bf7\u6c42\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6Python\u6570\u636e\u3002\u5e38\u7528\u7684AJAX\u5e93\u6709jQuery\u3001Axios\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528jQuery\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;AJAX Example&lt;\/title&gt;<\/p>\n<p>    &lt;script src=&quot;https:\/\/code.jquery.com\/jquery-3.6.0.min.js&quot;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;div id=&quot;data-container&quot;&gt;&lt;\/div&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        $(document).ready(function() {<\/p>\n<p>            $.ajax({<\/p>\n<p>                url: &#39;\/get-data&#39;,<\/p>\n<p>                method: &#39;GET&#39;,<\/p>\n<p>                success: function(response) {<\/p>\n<p>                    $(&#39;#data-container&#39;).html(&#39;&lt;p&gt;Name: &#39; + response.name + &#39;&lt;\/p&gt;&lt;p&gt;Age: &#39; + response.age + &#39;&lt;\/p&gt;&#39;);<\/p>\n<p>                }<\/p>\n<p>            });<\/p>\n<p>        });<\/p>\n<p>    &lt;\/script&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\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528jQuery\u7684<code>$.ajax<\/code>\u65b9\u6cd5\u5411\u670d\u52a1\u5668\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5e76\u5728\u6210\u529f\u63a5\u6536\u5230\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e\u540e\uff0c\u5c06\u5176\u663e\u793a\u5728\u9875\u9762\u7684<code>#data-container<\/code>\u5143\u7d20\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Flask\u4e2d\u5904\u7406AJAX\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u5728Flask\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u89c6\u56fe\u51fd\u6570\u6765\u5904\u7406AJAX\u8bf7\u6c42\uff0c\u5e76\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/get-data&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def get_data():<\/p>\n<p>    data = {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 28}<\/p>\n<p>    return jsonify(data)<\/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>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aFlask\u8def\u7531<code>\/get-data<\/code>\uff0c\u5b83\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\u3002\u901a\u8fc7<code>jsonify<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u5c06Python\u6570\u636e\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u4ee5\u4fbf\u524d\u7aefAJAX\u8bf7\u6c42\u80fd\u591f\u6b63\u786e\u89e3\u6790\u548c\u663e\u793a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528JINJA2\u6a21\u677f\u5f15\u64ce<\/p>\n<\/p>\n<p><p>Jinja2\u662f\u4e00\u4e2a\u73b0\u4ee3\u7684Python\u6a21\u677f\u5f15\u64ce\uff0c\u5e7f\u6cdb\u7528\u4e8eFlask\u548cDjango\u7b49Web\u6846\u67b6\u4e2d\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5728HTML\u4e2d\u5d4c\u5165Python\u4ee3\u7801\uff0c\u4ee5\u4fbf\u52a8\u6001\u751f\u6210\u7f51\u9875\u5185\u5bb9\u3002\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u6570\u636e\u4f20\u9012\u7ed9HTML\u6a21\u677f\uff0c\u5e76\u5728\u6a21\u677f\u4e2d\u8fdb\u884c\u6e32\u67d3\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Jinja2\u8bed\u6cd5\u57fa\u7840<\/strong><\/p>\n<\/p>\n<p><p>Jinja2\u6a21\u677f\u8bed\u6cd5\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u652f\u6301\u53d8\u91cf\u3001\u63a7\u5236\u7ed3\u6784\u3001\u8fc7\u6ee4\u5668\u7b49\u591a\u79cd\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684Jinja2\u8bed\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u53d8\u91cf<\/strong>\uff1a\u5728\u6a21\u677f\u4e2d\u4f7f\u7528<code>{{ variable_name }}<\/code>\u8bbf\u95eePython\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;p&gt;Name: {{ name }}&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;Age: {{ age }}&lt;\/p&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63a7\u5236\u7ed3\u6784<\/strong>\uff1a\u4f7f\u7528<code>{% %}<\/code>\u5b9a\u4e49\u63a7\u5236\u7ed3\u6784\uff0c\u5982\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">{% if age &gt; 18 %}<\/p>\n<p>&lt;p&gt;Adult&lt;\/p&gt;<\/p>\n<p>{% else %}<\/p>\n<p>&lt;p&gt;Minor&lt;\/p&gt;<\/p>\n<p>{% endif %}<\/p>\n<p><\/code><\/pre>\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<\/li>\n<li>\n<p><strong>\u8fc7\u6ee4\u5668<\/strong>\uff1a\u4f7f\u7528<code>|<\/code>\u7b26\u53f7\u5c06\u8fc7\u6ee4\u5668\u5e94\u7528\u4e8e\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;p&gt;Uppercase Name: {{ name | upper }}&lt;\/p&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5728Flask\u4e2d\u4f7f\u7528Jinja2\u6a21\u677f<\/strong><\/p>\n<\/p>\n<p><p>\u5728Flask\u4e2d\uff0c\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ea\u9700\u5c06Python\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>render_template<\/code>\u51fd\u6570\uff0c\u5e76\u5728\u6a21\u677f\u4e2d\u4f7f\u7528Jinja2\u8bed\u6cd5\u8fdb\u884c\u6e32\u67d3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    data = {&#39;name&#39;: &#39;Diana&#39;, &#39;age&#39;: 22}<\/p>\n<p>    return render_template(&#39;index.html&#39;, data=data)<\/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>\u5bf9\u5e94\u7684\u6a21\u677f\u6587\u4ef6<code>index.html<\/code>\u53ef\u80fd\u5982\u4e0b\u6240\u793a\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;Jinja2 Example&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Welcome, {{ data.name }}!&lt;\/h1&gt;<\/p>\n<p>    &lt;p&gt;Your age is {{ data.age }}.&lt;\/p&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\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728Flask\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8def\u7531<code>\/<\/code>\uff0c\u5e76\u5c06Python\u6570\u636e<code>data<\/code>\u4f20\u9012\u7ed9\u6a21\u677f<code>index.html<\/code>\u3002\u5728\u6a21\u677f\u4e2d\uff0c\u4f7f\u7528Jinja2\u8bed\u6cd5\u8bbf\u95ee\u5e76\u663e\u793a\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u5c06Python\u6570\u636e\u663e\u793a\u5728HTML\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u6839\u636e\u9879\u76ee\u7684\u590d\u6742\u5ea6\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u603b\u7ed3\u548c\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6<\/strong>\uff1a\u6839\u636e\u9879\u76ee\u89c4\u6a21\u548c\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u7684Web\u6846\u67b6\u3002Flask\u9002\u5408\u4e2d\u5c0f\u578b\u9879\u76ee\uff0c\u800cDjango\u9002\u5408\u5927\u578b\u9879\u76ee\u548c\u590d\u6742\u5e94\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6a21\u677f\u5f15\u64ce<\/strong>\uff1a\u5728HTML\u6a21\u677f\u4e2d\u4f7f\u7528Jinja2\u7b49\u6a21\u677f\u5f15\u64ce\uff0c\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u6e32\u67d3\u548c\u6570\u636e\u7ed1\u5b9a\u3002\u719f\u6089\u6a21\u677f\u5f15\u64ce\u7684\u8bed\u6cd5\u548c\u529f\u80fd\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u52a8\u6001\u66f4\u65b0<\/strong>\uff1a\u901a\u8fc7AJAX\u6280\u672f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7f51\u9875\u5185\u5bb9\u7684\u52a8\u6001\u66f4\u65b0\uff0c\u800c\u65e0\u9700\u5237\u65b0\u6574\u4e2a\u9875\u9762\u3002\u8fd9\u5bf9\u4e8e\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u548c\u6027\u80fd\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u5b89\u5168\u6027<\/strong>\uff1a\u5728\u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u65f6\uff0c\u6ce8\u610f\u9632\u8303XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff09\u7b49\u5b89\u5168\u95ee\u9898\u3002\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u65f6\uff0c\u9ed8\u8ba4\u4f1a\u5bf9\u8f93\u51fa\u8fdb\u884c\u8f6c\u4e49\uff0c\u4ee5\u9632\u6b62XSS\u653b\u51fb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u6027\u80fd<\/strong>\uff1a\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u590d\u6742\u903b\u8f91\u65f6\uff0c\u6ce8\u610f\u4f18\u5316\u540e\u7aef\u4ee3\u7801\u548c\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u7b49\u6280\u672f\u52a0\u901f\u9875\u9762\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06Python\u6570\u636e\u663e\u793a\u5728HTML\u4e2d\uff0c\u6784\u5efa\u51fa\u529f\u80fd\u5f3a\u5927\u3001\u7528\u6237\u4f53\u9a8c\u826f\u597d\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u6570\u636e\u4f20\u9012\u5230HTML\u4e2d\uff1f<\/strong><br \/>\u8981\u5728HTML\u4e2d\u663e\u793aPython\u6570\u636e\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u4e00\u4e2aWeb\u6846\u67b6\uff0c\u4f8b\u5982Flask\u6216Django\u3002\u8fd9\u4e9b\u6846\u67b6\u5141\u8bb8\u4f60\u5c06Python\u4e2d\u7684\u6570\u636e\u4f20\u9012\u5230HTML\u6a21\u677f\u4e2d\u3002\u901a\u8fc7\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\u5e76\u4f7f\u7528\u6a21\u677f\u6e32\u67d3\uff0cPython\u6570\u636e\u53ef\u4ee5\u8f7b\u677e\u5d4c\u5165\u5230HTML\u9875\u9762\u4e2d\u3002<\/p>\n<p><strong>\u4f7f\u7528Flask\u6846\u67b6\u65f6\uff0c\u5982\u4f55\u5c06\u6570\u636e\u5d4c\u5165HTML\uff1f<\/strong><br \/>\u5728Flask\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u6765\u5b9e\u73b0\u6570\u636e\u5d4c\u5165\u3002\u9996\u5148\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u51c6\u5907\u6570\u636e\uff0c\u7136\u540e\u5c06\u6570\u636e\u4f20\u9012\u7ed9<code>render_template<\/code>\u51fd\u6570\uff0c\u6700\u540e\u5728HTML\u6a21\u677f\u4e2d\u901a\u8fc7<code>{{ variable_name }}<\/code>\u7684\u65b9\u5f0f\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7JavaScript\u5728HTML\u4e2d\u52a8\u6001\u663e\u793aPython\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5c06Python\u6570\u636e\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5229\u7528JavaScript\u7684<code>fetch<\/code> API\u6216AJAX\u8bf7\u6c42\u6765\u5b9e\u73b0\u52a8\u6001\u663e\u793a\u3002\u540e\u7aef\u4f7f\u7528Flask\u6216Django\u63d0\u4f9b\u4e00\u4e2aAPI\u63a5\u53e3\uff0c\u524d\u7aef\u901a\u8fc7JavaScript\u83b7\u53d6\u6570\u636e\u5e76\u6e32\u67d3\u5230HTML\u4e2d\uff0c\u8fd9\u6837\u5b9e\u73b0\u4e86\u524d\u540e\u7aef\u7684\u52a8\u6001\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"HTML\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u663e\u793aPython\u6570\u636e\uff1a\u4f7f\u7528Flask\/Django\u7b49Web\u6846\u67b6\u3001\u901a\u8fc7AJAX\u4e0e\u540e\u7aef\u901a\u4fe1 [&hellip;]","protected":false},"author":3,"featured_media":1005296,"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\/1005288"}],"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=1005288"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005288\/revisions"}],"predecessor-version":[{"id":1005298,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005288\/revisions\/1005298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1005296"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1005288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1005288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1005288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}