{"id":18,"date":"2026-11-08T13:15:46","date_gmt":"2026-11-08T13:15:46","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=18"},"modified":"2026-11-08T13:15:46","modified_gmt":"2026-11-08T13:15:46","slug":"flask-%e4%bb%8e%e8%b7%af%e7%94%b1%e5%88%b0%e6%a8%a1%e6%9d%bf%ef%bc%9a%e5%81%9a%e4%b8%80%e4%b8%aa%e8%bf%b7%e4%bd%a0%e7%ab%99%e7%82%b9","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=18","title":{"rendered":"Flask \u4ece\u8def\u7531\u5230\u6a21\u677f\uff1a\u505a\u4e00\u4e2a\u8ff7\u4f60\u7ad9\u70b9"},"content":{"rendered":"<p>Flask \u8f7b\u91cf\u3001\u597d\u4e0a\u624b\uff0c\u5f88\u9002\u5408\u628a Python \u80fd\u529b\u63a5\u5230 Web \u4e0a\u3002\u672c\u6587\u4ece\u8def\u7531\u3001\u8bf7\u6c42\u3001\u6a21\u677f\u4e09\u5757\u642d\u4e00\u4e2a\u8ff7\u4f60\u7ad9\u70b9\u9aa8\u67b6\u3002<\/p>\n<h2>\u6700\u5c0f\u5e94\u7528<\/h2>\n<pre><code>from flask import Flask, render_template, request, redirect, url_for\n\napp = Flask(__name__)\n\n@app.get(\"\/\")\ndef index():\n    return render_template(\"index.html\", title=\"\u9996\u9875\")\n\n@app.route(\"\/hello\/&lt;name&gt;\")\ndef hello(name: str):\n    return f\"\u4f60\u597d\uff0c{name}\uff01\"\n\n@app.post(\"\/search\")\ndef search():\n    q = request.form.get(\"q\", \"\").strip()\n    return render_template(\"search.html\", q=q)\n\nif __name__ == \"__main__\":\n    app.run(debug=True, port=5000)\n<\/code><\/pre>\n<h2>\u6a21\u677f\u7ee7\u627f<\/h2>\n<pre><code>{# templates\/base.html #}\n&lt;!doctype html&gt;\n&lt;title&gt;{% block title %}\u7ad9\u70b9{% endblock %}&lt;\/title&gt;\n&lt;body&gt;\n  &lt;nav&gt;&lt;a href=\"{{ url_for('index') }}\"&gt;\u9996\u9875&lt;\/a&gt;&lt;\/nav&gt;\n  {% block content %}{% endblock %}\n&lt;\/body&gt;\n<\/code><\/pre>\n<pre><code>{# templates\/index.html #}\n{% extends \"base.html\" %}\n{% block title %}\u9996\u9875{% endblock %}\n{% block content %}\n&lt;h1&gt;\u6b22\u8fce&lt;\/h1&gt;\n{% endblock %}\n<\/code><\/pre>\n<h2>\u9879\u76ee\u7ed3\u6784\u5efa\u8bae<\/h2>\n<pre><code>myapp\/\n  app.py\n  templates\/\n  static\/\n  requirements.txt\n<\/code><\/pre>\n<h2>\u4e0b\u4e00\u6b65<\/h2>\n<ol>\n<li>\u628a\u4e1a\u52a1\u62c6\u6210 Blueprint\uff08\u7528\u6237\u3001\u6587\u7ae0\u3001\u540e\u53f0\uff09<\/li>\n<li>\u7528 Flask-Session \/ JWT \u505a\u767b\u5f55\u6001<\/li>\n<li>\u914d\u7f6e\u4e0e\u5bc6\u94a5\u8d70\u73af\u5883\u53d8\u91cf<\/li>\n<\/ol>\n<p>\u5148\u8dd1\u901a\u300c\u9875\u9762\u80fd\u5f00\u3001\u8868\u5355\u80fd\u4ea4\u3001\u6a21\u677f\u80fd\u590d\u7528\u300d\uff0c\u518d\u8c08\u6846\u67b6\u5bf9\u6bd4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flask \u8f7b\u91cf\u3001\u597d\u4e0a\u624b\uff0c\u5f88\u9002\u5408\u628a Python \u80fd\u529b\u63a5\u5230 Web \u4e0a\u3002\u672c\u6587\u4ece\u8def\u7531\u3001\u8bf7\u6c42\u3001\u6a21\u677f\u4e09\u5757\u642d\u4e00\u4e2a\u8ff7\u4f60\u7ad9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-web-development"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}