{"id":1125380,"date":"2025-01-08T19:51:25","date_gmt":"2025-01-08T11:51:25","guid":{"rendered":""},"modified":"2025-01-08T19:51:28","modified_gmt":"2025-01-08T11:51:28","slug":"%e5%a6%82%e4%bd%95%e6%96%b0%e5%bb%ba%e4%b8%80%e4%b8%aapython%e9%a1%b9%e7%9b%ae%e9%87%8c%e7%9a%84%e9%a1%b5%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1125380.html","title":{"rendered":"\u5982\u4f55\u65b0\u5efa\u4e00\u4e2apython\u9879\u76ee\u91cc\u7684\u9875\u9762"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090104\/8412af8d-b5e9-4ddf-b21f-8786056bc993.webp\" alt=\"\u5982\u4f55\u65b0\u5efa\u4e00\u4e2apython\u9879\u76ee\u91cc\u7684\u9875\u9762\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u65b0\u5efa\u4e00\u4e2aPython\u9879\u76ee\u91cc\u7684\u9875\u9762<\/strong><\/p>\n<\/p>\n<p><p>\u65b0\u5efa\u4e00\u4e2aPython\u9879\u76ee\u91cc\u7684\u9875\u9762\u53ef\u4ee5\u901a\u8fc7<strong>\u4f7f\u7528\u6846\u67b6\u3001\u6784\u5efa\u76ee\u5f55\u7ed3\u6784\u3001\u7f16\u5199\u89c6\u56fe\u51fd\u6570\u3001\u521b\u5efa\u6a21\u677f\u6587\u4ef6<\/strong>\u7b49\u6b65\u9aa4\u5b9e\u73b0\u3002<strong>\u4f7f\u7528\u6846\u67b6<\/strong>\u662f\u5176\u4e2d\u6700\u57fa\u7840\u7684\u6b65\u9aa4\uff0c\u56e0\u4e3a\u6846\u67b6\u4e0d\u4ec5\u7b80\u5316\u4e86\u5f00\u53d1\u8fc7\u7a0b\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u6269\u5c55\u6027\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Flask\u6846\u67b6\u6765\u65b0\u5efa\u4e00\u4e2aPython\u9879\u76ee\u9875\u9762\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6<\/h3>\n<\/p>\n<p><h4>1.1 Flask\u6846\u67b6\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u7279\u522b\u9002\u5408\u5c0f\u578b\u9879\u76ee\u548c\u539f\u578b\u5f00\u53d1\u3002\u5b83\u63d0\u4f9b\u4e86\u8def\u7531\u3001\u6a21\u677f\u6e32\u67d3\u3001\u8bf7\u6c42\u5904\u7406\u7b49\u57fa\u672c\u529f\u80fd\uff0c\u5e76\u4e14\u901a\u8fc7\u6269\u5c55\u53ef\u4ee5\u5b9e\u73b0\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4e3a\u4ec0\u4e48\u9009\u62e9Flask<\/h4>\n<\/p>\n<p><p><strong>\u6613\u7528\u6027<\/strong>\uff1aFlask\u7684\u5b66\u4e60\u66f2\u7ebf\u8f83\u4f4e\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/p>\n<p><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u4e0d\u5f3a\u5236\u4f7f\u7528\u7279\u5b9a\u7684\u9879\u76ee\u7ed3\u6784\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u3002<\/p>\n<p><strong>\u793e\u533a\u652f\u6301<\/strong>\uff1a\u6709\u5927\u91cf\u7684\u63d2\u4ef6\u548c\u6269\u5c55\u5e93\uff0c\u80fd\u6ee1\u8db3\u5404\u79cd\u529f\u80fd\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b89\u88c5\u5e76\u914d\u7f6eFlask<\/h3>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5Flask<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Flask\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u521b\u5efa\u9879\u76ee\u76ee\u5f55\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u76ee\u5f55\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa\u5fc5\u8981\u7684\u5b50\u76ee\u5f55\u548c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">my_flask_app\/<\/p>\n<p>    \u251c\u2500\u2500 app.py<\/p>\n<p>    \u251c\u2500\u2500 templates\/<\/p>\n<p>    \u2502   \u2514\u2500\u2500 index.html<\/p>\n<p>    \u2514\u2500\u2500 static\/<\/p>\n<p>        \u2514\u2500\u2500 style.css<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u89c6\u56fe\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>3.1 \u521b\u5efa\u57fa\u672c\u7684Flask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5728<code>app.py<\/code>\u6587\u4ef6\u4e2d\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\u4ee5\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Flask\u5e94\u7528\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 home():<\/p>\n<p>    return render_template(&#39;index.html&#39;)<\/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><h4>3.2 \u6dfb\u52a0\u66f4\u591a\u8def\u7531<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5173\u4e8e\u9875\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/about&#39;)<\/p>\n<p>def about():<\/p>\n<p>    return render_template(&#39;about.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u521b\u5efa\u6a21\u677f\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>4.1 \u7f16\u5199\u57fa\u672c\u7684HTML\u6a21\u677f<\/h4>\n<\/p>\n<p><p>\u5728<code>templates<\/code>\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>index.html<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u7f16\u5199\u57fa\u672c\u7684HTML\u4ee3\u7801\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>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<\/p>\n<p>    &lt;title&gt;Home Page&lt;\/title&gt;<\/p>\n<p>    &lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url_for(&#39;static&#39;, filename=&#39;style.css&#39;) }}&quot;&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Welcome to My Flask App&lt;\/h1&gt;<\/p>\n<p>    &lt;p&gt;This is the home page.&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u521b\u5efa\u66f4\u591a\u7684\u6a21\u677f\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u9700\u8981\u521b\u5efa\u66f4\u591a\u7684\u6a21\u677f\u6587\u4ef6\uff0c\u4f8b\u5982<code>about.html<\/code>\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>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<\/p>\n<p>    &lt;title&gt;About Page&lt;\/title&gt;<\/p>\n<p>    &lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url_for(&#39;static&#39;, filename=&#39;style.css&#39;) }}&quot;&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;About Us&lt;\/h1&gt;<\/p>\n<p>    &lt;p&gt;This is the about page.&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u9759\u6001\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>5.1 \u6dfb\u52a0CSS\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728<code>static<\/code>\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>style.css<\/code>\u7684CSS\u6587\u4ef6\uff0c\u5e76\u7f16\u5199\u4e00\u4e9b\u57fa\u672c\u7684\u6837\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">body {<\/p>\n<p>    font-family: Arial, sans-serif;<\/p>\n<p>    margin: 0;<\/p>\n<p>    padding: 0;<\/p>\n<p>    background-color: #f0f0f0;<\/p>\n<p>}<\/p>\n<p>h1 {<\/p>\n<p>    color: #333;<\/p>\n<p>}<\/p>\n<p>p {<\/p>\n<p>    color: #666;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u5f15\u5165\u9759\u6001\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728HTML\u6a21\u677f\u4e2d\u5f15\u5165CSS\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url_for(&#39;static&#39;, filename=&#39;style.css&#39;) }}&quot;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8fd0\u884cFlask\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5b8c\u6210\u4ee5\u4e0a\u6b65\u9aa4\u540e\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884cFlask\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python app.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8bbf\u95ee<code>http:\/\/127.0.0.1:5000<\/code>\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u65b0\u521b\u5efa\u7684\u9875\u9762\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u65b0\u5efa\u4e00\u4e2aPython\u9879\u76ee\u91cc\u7684\u9875\u9762\u3002\u9996\u5148\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\uff08\u5982Flask\uff09\uff0c\u7136\u540e\u5b89\u88c5\u5e76\u914d\u7f6e\u6846\u67b6\uff0c\u63a5\u7740\u7f16\u5199\u89c6\u56fe\u51fd\u6570\u548c\u521b\u5efa\u6a21\u677f\u6587\u4ef6\uff0c\u6700\u540e\u4f7f\u7528\u9759\u6001\u6587\u4ef6\u6765\u7f8e\u5316\u9875\u9762\u3002\u5e0c\u671b\u8fd9\u4e9b\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u80fd\u591f\u5e2e\u52a9\u4f60\u5feb\u901f\u4e0a\u624b\u5e76\u521b\u5efa\u51fa\u5c5e\u4e8e\u81ea\u5df1\u7684Python\u9879\u76ee\u9875\u9762\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u9879\u76ee\u4e2d\u6dfb\u52a0\u65b0\u7684\u9875\u9762\uff1f<\/strong><br \/>\u5728Python\u9879\u76ee\u4e2d\u6dfb\u52a0\u65b0\u9875\u9762\u7684\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u521b\u5efa\u65b0\u7684Python\u6587\u4ef6\u5e76\u8bbe\u7f6e\u9002\u5f53\u7684\u8def\u7531\u3002\u5982\u679c\u4f7f\u7528\u6846\u67b6\u5982Flask\u6216Django\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5728\u9879\u76ee\u4e2d\u914d\u7f6e\u597d\u89c6\u56fe\u548c\u6a21\u677f\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u89c6\u56fe\u51fd\u6570\uff0c\u5e76\u5728URL\u8def\u7531\u4e2d\u94fe\u63a5\u5230\u8fd9\u4e2a\u51fd\u6570\uff0c\u540c\u65f6\u4e3a\u8be5\u89c6\u56fe\u8bbe\u8ba1\u5bf9\u5e94\u7684HTML\u6a21\u677f\u3002<\/p>\n<p><strong>\u4f7f\u7528Flask\u6846\u67b6\u65f6\uff0c\u5982\u4f55\u521b\u5efa\u65b0\u9875\u9762\u7684\u6a21\u677f\uff1f<\/strong><br \/>\u5728Flask\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728templates\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684HTML\u6587\u4ef6\uff0c\u4f5c\u4e3a\u65b0\u9875\u9762\u7684\u6a21\u677f\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u4f7f\u7528render_template()\u65b9\u6cd5\u6765\u6e32\u67d3\u8fd9\u4e2a\u6a21\u677f\u3002\u786e\u4fdd\u5728\u9879\u76ee\u7684\u6587\u4ef6\u7ed3\u6784\u4e2d\u4fdd\u6301\u6574\u6d01\uff0c\u4ee5\u4fbf\u4e8e\u7ba1\u7406\u548c\u67e5\u627e\u5404\u4e2a\u9875\u9762\u3002<\/p>\n<p><strong>\u5728Django\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u8bbe\u7f6e\u65b0\u9875\u9762\u7684URL\u8def\u7531\uff1f<\/strong><br \/>\u5728Django\u4e2d\uff0c\u60a8\u9700\u8981\u5728urls.py\u6587\u4ef6\u4e2d\u6dfb\u52a0\u65b0\u7684URL\u6a21\u5f0f\uff0c\u4ee5\u4fbf\u5c06\u65b0\u7684\u89c6\u56fe\u4e0e\u7279\u5b9a\u7684URL\u5173\u8054\u3002\u521b\u5efa\u89c6\u56fe\u51fd\u6570\u540e\uff0c\u4f7f\u7528path()\u6216re_path()\u51fd\u6570\u5c06\u5176\u94fe\u63a5\u5230\u6240\u9700\u7684URL\u3002\u6b64\u8fc7\u7a0b\u786e\u4fdd\u7528\u6237\u5728\u8bbf\u95ee\u65b0\u9875\u9762\u65f6\u80fd\u591f\u6b63\u786e\u52a0\u8f7d\u76f8\u5173\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u65b0\u5efa\u9875\u9762\u7684SEO\u4f18\u5316\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f18\u5316\u65b0\u9875\u9762\u7684SEO\uff0c\u60a8\u9700\u8981\u5173\u6ce8\u9875\u9762\u6807\u9898\u3001\u63cf\u8ff0\u548c\u5173\u952e\u5b57\u7684\u8bbe\u7f6e\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5408\u9002\u7684HTML\u6807\u7b7e\uff08\u5982H1\u3001H2\u7b49\uff09\u6765\u7ec4\u7ec7\u5185\u5bb9\uff0c\u4ee5\u53ca\u786e\u4fdd\u9875\u9762\u52a0\u8f7d\u901f\u5ea6\u5feb\u548c\u79fb\u52a8\u8bbe\u5907\u53cb\u597d\u3002\u6dfb\u52a0\u76f8\u5173\u7684\u5185\u90e8\u548c\u5916\u90e8\u94fe\u63a5\u4e5f\u80fd\u63d0\u9ad8\u9875\u9762\u5728\u641c\u7d22\u5f15\u64ce\u4e2d\u7684\u53ef\u89c1\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u65b0\u5efa\u4e00\u4e2aPython\u9879\u76ee\u91cc\u7684\u9875\u9762 \u65b0\u5efa\u4e00\u4e2aPython\u9879\u76ee\u91cc\u7684\u9875\u9762\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6846\u67b6\u3001\u6784\u5efa\u76ee\u5f55\u7ed3\u6784\u3001\u7f16\u5199\u89c6\u56fe [&hellip;]","protected":false},"author":3,"featured_media":1125389,"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\/1125380"}],"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=1125380"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125380\/revisions"}],"predecessor-version":[{"id":1125391,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125380\/revisions\/1125391"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1125389"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}