{"id":165,"date":"2026-06-17T19:17:59","date_gmt":"2026-06-17T11:17:59","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=165"},"modified":"2026-06-17T19:17:59","modified_gmt":"2026-06-17T11:17:59","slug":"flask-%e8%b7%af%e7%94%b1","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/17\/flask-%e8%b7%af%e7%94%b1\/","title":{"rendered":"Flask \u8def\u7531"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 Flask \u4e2d\uff0c<strong>\u8def\u7531\uff08Routing\uff09<\/strong> \u662f Web \u6846\u67b6\u6700\u6838\u5fc3\u7684\u673a\u5236\u4e4b\u4e00\u3002\u5b83\u7684\u4f5c\u7528\u662f\u5efa\u7acb <strong>URL \u8def\u5f84<\/strong> \u4e0e <strong>Python \u89c6\u56fe\u51fd\u6570<\/strong> \u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u8def\u7531\u5c31\u662f\u544a\u8bc9 Flask\uff1a\u201c\u5f53\u7528\u6237\u8bbf\u95ee\u8fd9\u4e2a URL \u65f6\uff0c\u5e94\u8be5\u6267\u884c\u54ea\u6bb5\u4ee3\u7801\u201d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f Flask \u8def\u7531\u4ece\u57fa\u7840\u5230\u8fdb\u9636\u7684\u5b8c\u6574\u6307\u5357\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u57fa\u7840\u8def\u7531<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u57fa\u7840\u7684\u8def\u7531\u4f7f\u7528 <code>@app.route()<\/code> \u88c5\u9970\u5668\uff0c\u5c06 URL \u7ed1\u5b9a\u5230\u51fd\u6570\u4e0a\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from flask import Flask\n\napp = Flask(__name__)\n\n# \u5f53\u7528\u6237\u8bbf\u95ee\u6839\u8def\u5f84 &#039;\/&#039; \u65f6\uff0c\u6267\u884c index \u51fd\u6570\n@app.route(&#039;\/&#039;)\ndef index():\n    return &#039;\u6b22\u8fce\u6765\u5230\u9996\u9875\uff01&#039;\n\n# \u5f53\u7528\u6237\u8bbf\u95ee &#039;\/about&#039; \u65f6\uff0c\u6267\u884c about \u51fd\u6570\n@app.route(&#039;\/about&#039;)\ndef about():\n    return &#039;\u8fd9\u662f\u5173\u4e8e\u6211\u4eec\u7684\u9875\u9762\u3002&#039;<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u52a8\u6001\u8def\u7531\uff08\u53d8\u91cf\u89c4\u5219\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f88\u591a\u65f6\u5019\uff0cURL \u4e2d\u5305\u542b\u52a8\u6001\u53d8\u5316\u7684\u90e8\u5206\uff08\u4f8b\u5982\u7528\u6237 ID\u3001\u6587\u7ae0 ID\uff09\u3002Flask \u5141\u8bb8\u4f60\u5728 URL \u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u89c6\u56fe\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bed\u6cd5\uff1a<code>&lt;\u53d8\u91cf\u540d&gt;<\/code><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u63d0\u53d6 URL \u4e2d\u7684 username\n@app.route(&#039;\/user\/&lt;username&gt;&#039;)\ndef show_user_profile(username):\n    return f&#039;\u7528\u6237: {username}&#039;<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u8f6c\u6362\u5668\uff08Converters\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cURL \u4e2d\u7684\u53d8\u91cf\u662f<strong>\u5b57\u7b26\u4e32<\/strong>\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8f6c\u6362\u5668\u6765\u6307\u5b9a\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002\u5982\u679c\u7c7b\u578b\u4e0d\u5339\u914d\uff0cFlask \u4f1a\u8fd4\u56de 404\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bed\u6cd5\uff1a<code>&lt;\u8f6c\u6362\u5668:\u53d8\u91cf\u540d&gt;<\/code><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># int: \u53ea\u5339\u914d\u6574\u6570\n@app.route(&#039;\/post\/&lt;int:post_id&gt;&#039;)\ndef show_post(post_id):\n    # \u8fd9\u91cc\u7684 post_id \u5df2\u7ecf\u662f int \u7c7b\u578b\uff0c\u65e0\u9700\u624b\u52a8 int() \u8f6c\u6362\n    return f&#039;\u6587\u7ae0 ID: {post_id}&#039;\n\n# float: \u5339\u914d\u6d6e\u70b9\u6570\n@app.route(&#039;\/price\/&lt;float:amount&gt;&#039;)\ndef show_price(amount):\n    return f&#039;\u4ef7\u683c: {amount}&#039;\n\n# path: \u7c7b\u4f3c string\uff0c\u4f46\u5141\u8bb8\u5305\u542b\u659c\u6760 &#039;\/&#039; (\u5e38\u7528\u4e8e\u6587\u4ef6\u8def\u5f84)\n@app.route(&#039;\/files\/&lt;path:filepath&gt;&#039;)\ndef show_file(filepath):\n    return f&#039;\u6587\u4ef6\u8def\u5f84: {filepath}&#039;\n\n# uuid: \u5339\u914d UUID \u5b57\u7b26\u4e32\n@app.route(&#039;\/uuid\/&lt;uuid:identifier&gt;&#039;)\ndef show_uuid(identifier):\n    return f&#039;UUID: {identifier}&#039;<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u552f\u4e00\u659c\u6760\u884c\u4e3a\uff08Trailing Slash\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u4e00\u4e2a\u65b0\u624b\u7ecf\u5e38\u8e29\u5751\u7684\u5730\u65b9\u3002Flask \u5bf9 URL \u672b\u5c3e\u7684\u659c\u6760 <code>\/<\/code> \u6709\u4e25\u683c\u7684\u5904\u7406\u89c4\u5219\uff0c\u7c7b\u4f3c\u4e8e\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u201c\u6587\u4ef6\u201d\u548c\u201c\u76ee\u5f55\u201d\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u89c4\u5219 1\uff1a\u8def\u7531\u672b\u5c3e\u6709\u659c\u6760 (\u7c7b\u4f3c\u76ee\u5f55)\n@app.route(&#039;\/users\/&#039;)\ndef users():\n    return &#039;\u7528\u6237\u5217\u8868&#039;\n# \u8bbf\u95ee \/users \u65f6\uff0cFlask \u4f1a\u81ea\u52a8 308 \u91cd\u5b9a\u5411\u5230 \/users\/\n\n# \u89c4\u5219 2\uff1a\u8def\u7531\u672b\u5c3e\u6ca1\u6709\u659c\u6760 (\u7c7b\u4f3c\u6587\u4ef6)\n@app.route(&#039;\/profile&#039;)\ndef profile():\n    return &#039;\u4e2a\u4eba\u4e3b\u9875&#039;\n# \u8bbf\u95ee \/profile\/ \u65f6\uff0cFlask \u4f1a\u76f4\u63a5\u8fd4\u56de 404 Not Found<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u4fdd\u6301\u56e2\u961f\u89c4\u8303\u7edf\u4e00\u3002\u901a\u5e38\u5efa\u8bae API \u63a5\u53e3\u4e0d\u52a0\u672b\u5c3e\u659c\u6760\uff0c\u800c\u4f20\u7edf\u7684\u7f51\u9875\u8def\u7531\u52a0\u4e0a\u672b\u5c3e\u659c\u6760\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6307\u5b9a HTTP \u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8def\u7531\u53ea\u54cd\u5e94 <strong>GET<\/strong> \u8bf7\u6c42\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u8868\u5355\u63d0\u4ea4\uff08POST\uff09\u6216\u5176\u4ed6\u65b9\u6cd5\uff0c\u5fc5\u987b\u663e\u5f0f\u6307\u5b9a <code>methods<\/code> \u53c2\u6570\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from flask import request\n\n@app.route(&#039;\/login&#039;, methods=[&#039;GET&#039;, &#039;POST&#039;])\ndef login():\n    if request.method == &#039;POST&#039;:\n        # \u5904\u7406\u767b\u5f55\u903b\u8f91 (\u83b7\u53d6\u8868\u5355\u6570\u636e)\n        username = request.form.get(&#039;username&#039;)\n        return f&#039;\u6b63\u5728\u767b\u5f55: {username}&#039;\n    else:\n        # \u8fd4\u56de\u767b\u5f55\u8868\u5355\u9875\u9762\n        return &#039;\u663e\u793a\u767b\u5f55\u8868\u5355 (GET)&#039;<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u652f\u6301\u7684\u5e38\u7528\u65b9\u6cd5\uff1a<code>GET<\/code>, <code>POST<\/code>, <code>PUT<\/code>, <code>DELETE<\/code>, <code>PATCH<\/code>, <code>HEAD<\/code>, <code>OPTIONS<\/code>\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. URL \u6784\u5efa\uff08\u53cd\u5411\u89e3\u6790\uff09\u2b50 \u6838\u5fc3\u91cd\u70b9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6c38\u8fdc\u4e0d\u8981\u5728\u4ee3\u7801\u4e2d\u786c\u7f16\u7801 URL\uff01<\/strong><br>Flask \u63d0\u4f9b\u4e86 <code>url_for()<\/code> \u51fd\u6570\uff0c\u5b83\u901a\u8fc7<strong>\u89c6\u56fe\u51fd\u6570\u7684\u540d\u5b57<\/strong>\uff08\u5373\u7aef\u70b9 Endpoint\uff09\u6765\u53cd\u5411\u751f\u6210 URL\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from flask import url_for\n\n@app.route(&#039;\/&#039;)\ndef index():\n    # \u751f\u6210\u9759\u6001\u6587\u4ef6 URL\n    css_url = url_for(&#039;static&#039;, filename=&#039;style.css&#039;)\n\n    # \u751f\u6210\u52a8\u6001\u8def\u7531 URL (\u4f20\u5165\u53d8\u91cf\u53c2\u6570)\n    user_url = url_for(&#039;show_user_profile&#039;, username=&#039;john_doe&#039;)\n    post_url = url_for(&#039;show_post&#039;, post_id=42)\n\n    return f&quot;&quot;&quot;\n    CSS: {css_url}&lt;br&gt;\n    User: {user_url}&lt;br&gt;\n    Post: {post_url}\n    &quot;&quot;&quot;<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e3a\u4ec0\u4e48\u7528 <code>url_for<\/code>\uff1f<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u786c\u7f16\u7801<\/strong>\uff1a\u5982\u679c\u4ee5\u540e\u628a <code>\/post\/&lt;int:id><\/code> \u6539\u6210\u4e86 <code>\/article\/&lt;int:id><\/code>\uff0c\u53ea\u9700\u8981\u6539\u8def\u7531\u88c5\u9970\u5668\uff0c\u6240\u6709 <code>url_for('show_post')<\/code> \u7684\u5730\u65b9\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u8f6c\u4e49<\/strong>\uff1a\u5b83\u4f1a\u81ea\u52a8\u5904\u7406 URL \u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u3002<\/li>\n\n\n\n<li><strong>\u5904\u7406\u7edd\u5bf9\u8def\u5f84<\/strong>\uff1a\u53ea\u9700\u52a0\u4e0a <code>_external=True<\/code> \u53c2\u6570\uff08\u5982 <code>url_for('index', _external=True)<\/code>\uff09\uff0c\u5c31\u80fd\u751f\u6210\u5e26\u6709\u57df\u540d\u7684\u5b8c\u6574 URL\uff08\u5e38\u7528\u4e8e\u53d1\u9001\u90ae\u4ef6\u4e2d\u7684\u94fe\u63a5\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u9759\u6001\u6587\u4ef6\u8def\u7531<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Flask \u4f1a\u81ea\u52a8\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2a\u7279\u6b8a\u7684\u8def\u7531\u7528\u4e8e\u63d0\u4f9b\u9759\u6001\u6587\u4ef6\uff08CSS, JS, \u56fe\u7247\u7b49\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ea\u9700\u8981\u5728 <code>url_for<\/code> \u4e2d\u4f7f\u7528\u7aef\u70b9\u540d <code>'static'<\/code>\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u5047\u8bbe\u4f60\u7684\u9879\u76ee\u7ed3\u6784\u4e2d\u6709 static\/images\/logo.png\nurl_for(&#039;static&#039;, filename=&#039;images\/logo.png&#039;)\n# \u751f\u6210\u7684 URL \u4e3a: \/static\/images\/logo.png<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u8fdb\u9636\uff1a\u57fa\u4e8e\u7c7b\u7684\u89c6\u56fe (Pluggable Views)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u4e00\u4e2a URL \u9700\u8981\u6839\u636e\u4e0d\u540c\u7684 HTTP \u65b9\u6cd5\uff08GET, POST\uff09\u6267\u884c\u5b8c\u5168\u4e0d\u540c\u7684\u903b\u8f91\uff0c\u4f7f\u7528 <code>if\/else<\/code> \u4f1a\u663e\u5f97\u81c3\u80bf\u3002Flask \u63d0\u4f9b\u4e86\u57fa\u4e8e\u7c7b\u7684\u89c6\u56fe\uff08MethodView\uff09\u6765\u4f18\u96c5\u5730\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from flask.views import MethodView\n\nclass UserAPI(MethodView):\n    def get(self, user_id):\n        if user_id is None:\n            return &#039;\u83b7\u53d6\u7528\u6237\u5217\u8868&#039;\n        else:\n            return f&#039;\u83b7\u53d6\u7528\u6237 {user_id} \u7684\u8be6\u60c5&#039;\n\n    def post(self):\n        return &#039;\u521b\u5efa\u65b0\u7528\u6237&#039;\n\n    def delete(self, user_id):\n        return f&#039;\u5220\u9664\u7528\u6237 {user_id}&#039;\n\n# \u5c06\u7c7b\u89c6\u56fe\u6ce8\u518c\u5230\u8def\u7531\n# \u6ce8\u610f\uff1a\u9700\u8981\u8c03\u7528 .as_view(&#039;endpoint_name&#039;) \u65b9\u6cd5\nuser_view = UserAPI.as_view(&#039;user_api&#039;)\n\n# \u65e0\u53c2\u6570\u8def\u7531 (\u5904\u7406\u5217\u8868\u548c\u521b\u5efa)\napp.add_url_rule(&#039;\/users\/&#039;, view_func=user_view, methods=[&#039;GET&#039;, &#039;POST&#039;])\n# \u5e26\u53c2\u6570\u8def\u7531 (\u5904\u7406\u8be6\u60c5\u548c\u5220\u9664)\napp.add_url_rule(&#039;\/users\/&lt;int:user_id&gt;&#039;, view_func=user_view, methods=[&#039;GET&#039;, &#039;DELETE&#039;])<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u6ce8\uff1a\u5728\u5b9e\u9645\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u57fa\u4e8e\u7c7b\u7684\u89c6\u56fe\u5e38\u7528\u4e8e\u6784\u5efa RESTful API\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u8def\u7531\u6700\u4f73\u5b9e\u8df5\u603b\u7ed3<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7aef\u70b9\u547d\u540d<\/strong>\uff1a\u89c6\u56fe\u51fd\u6570\u7684\u540d\u5b57\uff08\u7aef\u70b9\uff09\u5e94\u8be5\u5177\u6709\u63cf\u8ff0\u6027\uff0c\u56e0\u4e3a <code>url_for<\/code> \u4f9d\u8d56\u5b83\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u4e1a\u52a1\u903b\u8f91\u8fc7\u91cd<\/strong>\uff1a\u8def\u7531\/\u89c6\u56fe\u51fd\u6570\u5e94\u8be5\u53ea\u8d1f\u8d23\u201c\u63a5\u6536\u8bf7\u6c42 -> \u8c03\u7528\u670d\u52a1\u5c42\u5904\u7406 -> \u8fd4\u56de\u54cd\u5e94\u201d\uff0c\u4e0d\u8981\u628a\u51e0\u767e\u884c\u7684\u6570\u636e\u5e93\u67e5\u8be2\u548c\u4e1a\u52a1\u903b\u8f91\u76f4\u63a5\u5199\u5728\u89c6\u56fe\u51fd\u6570\u91cc\u3002<\/li>\n\n\n\n<li><strong>\u5584\u7528\u84dd\u56fe<\/strong>\uff1a\u5f53\u8def\u7531\u8d85\u8fc7 10 \u4e2a\u65f6\uff0c\u7acb\u5373\u4f7f\u7528 <strong>\u84dd\u56fe\uff08Blueprint\uff09<\/strong> \u5c06\u8def\u7531\u62c6\u5206\u5230\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\uff08\u5982 <code>auth_routes.py<\/code>, <code>api_routes.py<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7edf\u4e00 API \u524d\u7f00<\/strong>\uff1a\u5728\u6ce8\u518c API \u84dd\u56fe\u65f6\uff0c\u4f7f\u7528 <code>url_prefix='\/api\/v1'<\/code>\uff0c\u65b9\u4fbf\u540e\u7eed\u7248\u672c\u8fed\u4ee3\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1\u4e86\u8def\u7531\uff0c\u4f60\u5c31\u638c\u63e1\u4e86\u63a7\u5236 Web \u5e94\u7528\u6d41\u91cf\u8d70\u5411\u7684\u201c\u65b9\u5411\u76d8\u201d\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u60f3\u4e86\u89e3\u5982\u4f55\u5904\u7406\u8def\u7531\u4e2d\u63a5\u6536\u5230\u7684<strong>\u8bf7\u6c42\u6570\u636e (Request)<\/strong>\uff0c\u8fd8\u662f\u5b66\u4e60\u5982\u4f55\u8fd4\u56de<strong>\u6a21\u677f (Templates)<\/strong> \u6216 <strong>JSON<\/strong>\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Flask \u4e2d\uff0c\u8def\u7531\uff08Routing\uff09 \u662f Web \u6846\u67b6&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-flask-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/165\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}