{"id":940489,"date":"2024-12-26T21:01:02","date_gmt":"2024-12-26T13:01:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940489.html"},"modified":"2024-12-26T21:01:04","modified_gmt":"2024-12-26T13:01:04","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8flask","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/940489.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528flask"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075110\/d9305014-a5c8-44d4-be2a-7bfcbb9edb2a.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528flask\" \/><\/p>\n<h2><strong>PYTHON\u5982\u4f55\u4f7f\u7528FLASK<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c<strong>Flask\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528<\/strong>\u3002\u5b83\u9002\u7528\u4e8e\u6784\u5efa\u4e2d\u5c0f\u578bWeb\u5e94\u7528\u7a0b\u5e8f\u548cAPI\u3002\u4f7f\u7528Flask\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u5b89\u88c5Flask\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u3001\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\u3001\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong>\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5185\u5bb9\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Flask\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5FLASK<\/h2>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Flask\uff0c\u9996\u5148\u9700\u8981\u5728\u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5Flask\uff0c\u8fd9\u662f\u4e00\u79cdPython\u5305\u7ba1\u7406\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Python\u548cpip<\/strong>\uff1a\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cpip\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u8bbf\u95eePython\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u5305\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165<code>python --version<\/code>\u548c<code>pip --version<\/code>\u6765\u9a8c\u8bc1\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Flask<\/strong>\uff1a\u6253\u5f00\u547d\u4ee4\u884c\u7ec8\u7aef\uff0c\u8f93\u5165\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><p>\u8fd9\u5c06\u4f1a\u4e0b\u8f7d\u5e76\u5b89\u88c5Flask\u4ee5\u53ca\u5b83\u7684\u4f9d\u8d56\u9879\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883\uff08\u53ef\u9009\uff09<\/strong>\uff1a\u5efa\u8bae\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fdb\u884c\u5f00\u53d1\uff0c\u4ee5\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5e93\u51b2\u7a81\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<ul>\n<li>Windows:\n<pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>macOS\/Linux:\n<pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h2>\u4e8c\u3001\u521b\u5efa\u7b80\u5355\u7684FLASK\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u7a0b\u5e8f\u53ea\u9700\u8981\u51e0\u884c\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684Flask\u5e94\u7528\u7a0b\u5e8f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u6587\u4ef6<\/strong>\uff1a\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>app.py<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Flask\u4ee3\u7801<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello_world():<\/p>\n<p>    return &#39;Hello, World!&#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<\/li>\n<li>\n<p><strong>\u8fd0\u884cFlask\u5e94\u7528\u7a0b\u5e8f<\/strong>\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u5bfc\u822a\u5230\u9879\u76ee\u76ee\u5f55\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python app.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728\u9ed8\u8ba4\u7684\u7aef\u53e35000\u4e0a\u542f\u52a8Flask\u5f00\u53d1\u670d\u52a1\u5668\u3002\u60a8\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<code>http:\/\/127.0.0.1:5000\/<\/code>\u6765\u67e5\u770b\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5728Flask\u4e2d\uff0c<strong>\u8def\u7531\u7528\u4e8e\u5b9a\u4e49URL\u8def\u5f84\uff0c\u5e76\u5c06\u5176\u6620\u5c04\u5230\u7279\u5b9a\u7684\u89c6\u56fe\u51fd\u6570<\/strong>\u3002\u89c6\u56fe\u51fd\u6570\u8d1f\u8d23\u5904\u7406\u8bf7\u6c42\u5e76\u8fd4\u56de\u54cd\u5e94\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u8def\u7531<\/strong>\uff1a\u901a\u8fc7\u4f7f\u7528<code>@app.route()<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u8def\u7531\u3002\u60a8\u53ef\u4ee5\u4e3a\u6bcf\u4e2aURL\u8def\u5f84\u5b9a\u4e49\u4e00\u4e2a\u89c6\u56fe\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/about&#39;)<\/p>\n<p>def about():<\/p>\n<p>    return &#39;This is the About page.&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8def\u7531\u53c2\u6570<\/strong>\uff1a\u53ef\u4ee5\u5728\u8def\u7531\u4e2d\u4f7f\u7528\u53c2\u6570\u6765\u52a8\u6001\u5904\u7406\u4e0d\u540c\u7684\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/user\/&lt;username&gt;&#39;)<\/p>\n<p>def show_user_profile(username):<\/p>\n<p>    return f&#39;User {username}&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>HTTP\u65b9\u6cd5<\/strong>\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cFlask\u89c6\u56fe\u51fd\u6570\u54cd\u5e94GET\u8bf7\u6c42\u3002\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570<code>methods<\/code>\u6307\u5b9a\u5176\u4ed6HTTP\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/login&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<\/p>\n<p>def login():<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        return &#39;Do the login&#39;<\/p>\n<p>    else:<\/p>\n<p>        return &#39;Show the login form&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u6a21\u677f\u6e32\u67d3<\/h2>\n<\/p>\n<p><p>Flask\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u6765\u6e32\u67d3HTML\u6a21\u677f\uff0c\u4ece\u800c\u4e3aWeb\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u52a8\u6001\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u6a21\u677f\u76ee\u5f55<\/strong>\uff1a\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>templates<\/code>\u7684\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653eHTML\u6a21\u677f\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199HTML\u6a21\u677f<\/strong>\uff1a\u5728<code>templates<\/code>\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a<code>index.html<\/code>\u6587\u4ef6\u3002<\/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&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Welcome to Flask&lt;\/h1&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6e32\u67d3\u6a21\u677f<\/strong>\uff1a\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u4f7f\u7528<code>render_template<\/code>\u51fd\u6570\u6765\u6e32\u67d3HTML\u6a21\u677f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import render_template<\/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><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u4e94\u3001\u5904\u7406\u8868\u5355\u6570\u636e<\/h2>\n<\/p>\n<p><p>Flask\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5904\u7406HTML\u8868\u5355\u6570\u636e\uff0c\u901a\u8fc7\u8bf7\u6c42\u5bf9\u8c61\u53ef\u4ee5\u83b7\u53d6\u8868\u5355\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u8868\u5355<\/strong>\uff1a\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\u521b\u5efa\u4e00\u4e2aHTML\u8868\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;form action=&quot;\/submit&quot; method=&quot;post&quot;&gt;<\/p>\n<p>    &lt;input type=&quot;text&quot; name=&quot;name&quot; placeholder=&quot;Enter your name&quot;&gt;<\/p>\n<p>    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8868\u5355\u6570\u636e<\/strong>\uff1a\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u4f7f\u7528<code>request.form<\/code>\u83b7\u53d6\u8868\u5355\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import request<\/p>\n<p>@app.route(&#39;\/submit&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def submit():<\/p>\n<p>    name = request.form[&#39;name&#39;]<\/p>\n<p>    return f&#39;Hello, {name}!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u516d\u3001\u9759\u6001\u6587\u4ef6\u7ba1\u7406<\/h2>\n<\/p>\n<p><p>Flask\u80fd\u591f\u8f7b\u677e\u7ba1\u7406\u9759\u6001\u6587\u4ef6\uff0c\u5982CSS\u3001JavaScript\u548c\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u9759\u6001\u6587\u4ef6\u76ee\u5f55<\/strong>\uff1a\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>static<\/code>\u7684\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653e\u9759\u6001\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5f15\u7528\u9759\u6001\u6587\u4ef6<\/strong>\uff1a\u5728HTML\u6a21\u677f\u4e2d\u4f7f\u7528<code>url_for<\/code>\u51fd\u6570\u5f15\u7528\u9759\u6001\u6587\u4ef6\u3002<\/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<\/li>\n<li>\n<p><strong>\u8bbf\u95ee\u9759\u6001\u6587\u4ef6<\/strong>\uff1a\u901a\u8fc7URL\u8def\u5f84<code>\/static\/&lt;filename&gt;<\/code>\u8bbf\u95ee\u9759\u6001\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u4e03\u3001\u4f7f\u7528FLASK\u6269\u5c55<\/h2>\n<\/p>\n<p><p>Flask\u652f\u6301\u591a\u79cd\u6269\u5c55\uff0c\u7528\u4e8e\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\uff0c\u5982\u6570\u636e\u5e93\u96c6\u6210\u3001\u8868\u5355\u9a8c\u8bc1\u3001\u7528\u6237\u8ba4\u8bc1\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Flask-SQLAlchemy<\/strong>\uff1a\u7528\u4e8e\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684ORM\u6846\u67b6\u3002\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Flask\u5e94\u7528\u4e2d\u7ba1\u7406\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Flask-WTF<\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u8868\u5355\u7684\u6269\u5c55\uff0c\u63d0\u4f9b\u8868\u5355\u9a8c\u8bc1\u548cCSRF\u4fdd\u62a4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Flask-Login<\/strong>\uff1a\u7528\u4e8e\u7ba1\u7406\u7528\u6237\u4f1a\u8bdd\u548c\u8ba4\u8bc1\u7684\u6269\u5c55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u6269\u5c55<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5Flask\u6269\u5c55\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask-SQLAlchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6269\u5c55<\/strong>\uff1a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask_sqlalchemy import SQLAlchemy<\/p>\n<p>app.config[&#39;SQLALCHEMY_DATABASE_URI&#39;] = &#39;sqlite:\/\/\/site.db&#39;<\/p>\n<p>db = SQLAlchemy(app)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u516b\u3001\u8c03\u8bd5\u548c\u6d4b\u8bd5<\/h2>\n<\/p>\n<p><p>Flask\u63d0\u4f9b\u4e86\u4e00\u4e9b\u529f\u80fd\u6765\u5e2e\u52a9\u8c03\u8bd5\u548c\u6d4b\u8bd5\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8c03\u8bd5\u6a21\u5f0f<\/strong>\uff1a\u542f\u7528\u8c03\u8bd5\u6a21\u5f0f\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u51fa\u73b0\u9519\u8bef\u65f6\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u548c\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528Python\u7684unittest\u6a21\u5757\u6765\u7f16\u5199\u548c\u8fd0\u884cFlask\u5e94\u7528\u7a0b\u5e8f\u7684\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>class FlaskTestCase(unittest.TestCase):<\/p>\n<p>    def test_home(self):<\/p>\n<p>        tester = app.test_client(self)<\/p>\n<p>        response = tester.get(&#39;\/&#39;)<\/p>\n<p>        self.assertEqual(response.status_code, 200)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5\u5ba2\u6237\u7aef<\/strong>\uff1aFlask\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u5728\u6d4b\u8bd5\u4e2d\u6a21\u62df\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with app.test_client() as client:<\/p>\n<p>    response = client.get(&#39;\/&#39;)<\/p>\n<p>    assert b&#39;Welcome to Flask&#39; in response.data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Flask\u8f7b\u677e\u6784\u5efa\u548c\u90e8\u7f72Web\u5e94\u7528\u7a0b\u5e8f\u3002Flask\u7684\u7b80\u6d01\u6027\u548c\u7075\u6d3b\u6027\u4f7f\u5176\u6210\u4e3a\u5f00\u53d1\u8005\u4eec\u7684\u9996\u9009\u6846\u67b6\u4e4b\u4e00\uff0c\u5c24\u5176\u662f\u5728\u5feb\u901f\u539f\u578b\u5f00\u53d1\u548c\u5c0f\u578b\u9879\u76ee\u4e2d\u3002\u5e0c\u671b\u672c\u6307\u5357\u80fd\u5e2e\u52a9\u60a8\u7406\u89e3\u5e76\u5f00\u59cb\u4f7f\u7528Flask\u8fdb\u884cWeb\u5f00\u53d1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Flask\u662f\u4ec0\u4e48\uff0c\u5b83\u6709\u4ec0\u4e48\u7279\u70b9\uff1f<\/strong><br \/>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python web\u6846\u67b6\uff0c\u8bbe\u8ba1\u4e0a\u4ee5\u7b80\u6d01\u548c\u6613\u7528\u4e3a\u6838\u5fc3\u3002\u5b83\u7684\u7279\u70b9\u5305\u62ec\u7075\u6d3b\u6027\u9ad8\u3001\u6a21\u5757\u5316\u8bbe\u8ba1\u4ee5\u53ca\u4e30\u5bcc\u7684\u6269\u5c55\u5e93\u3002Flask\u5141\u8bb8\u5f00\u53d1\u8005\u5feb\u901f\u642d\u5efaweb\u5e94\u7528\uff0c\u9002\u5408\u4ece\u5c0f\u578b\u9879\u76ee\u5230\u5927\u578b\u5e94\u7528\u7684\u5f00\u53d1\u9700\u6c42\u3002\u540c\u65f6\uff0c\u5b83\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u548c\u6a21\u677f\u5f15\u64ce\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u8981\u81ea\u7531\u9009\u62e9\u3002<\/p>\n<p><strong>\u4f7f\u7528Flask\u6784\u5efaweb\u5e94\u7528\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u6784\u5efaFlask\u5e94\u7528\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u9996\u5148\u5b89\u88c5Flask\u5e93\uff0c\u63a5\u7740\u521b\u5efaFlask\u5e94\u7528\u5b9e\u4f8b\uff0c\u7136\u540e\u5b9a\u4e49\u8def\u7531\uff0c\u6700\u540e\u7f16\u5199\u89c6\u56fe\u51fd\u6570\u6765\u5904\u7406\u7528\u6237\u8bf7\u6c42\u3002\u5177\u4f53\u6d41\u7a0b\u4e3a\uff1a\u5728\u5e94\u7528\u6587\u4ef6\u4e2d\u5bfc\u5165Flask\uff0c\u521b\u5efa\u5e94\u7528\u5b9e\u4f8b\uff0c\u4f7f\u7528<code>@app.route()<\/code>\u88c5\u9970\u5668\u5b9a\u4e49URL\u8def\u5f84\uff0c\u5e76\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u8fd4\u56de\u5185\u5bb9\uff0c\u6700\u540e\u901a\u8fc7\u8fd0\u884c<code>app.run()<\/code>\u542f\u52a8\u670d\u52a1\u5668\u3002<\/p>\n<p><strong>Flask\u4e0e\u5176\u4ed6Python web\u6846\u67b6\u76f8\u6bd4\u6709\u54ea\u4e9b\u4f18\u52bf\u548c\u52a3\u52bf\uff1f<\/strong><br \/>Flask\u76f8\u8f83\u4e8eDjango\u7b49\u5176\u4ed6Python web\u6846\u67b6\uff0c\u5176\u4f18\u52bf\u5728\u4e8e\u8f7b\u91cf\u7ea7\u548c\u7075\u6d3b\u6027\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u81ea\u7531\u9009\u62e9\u7ec4\u4ef6\uff0c\u800c\u4e0d\u53d7\u6846\u67b6\u7684\u7ea6\u675f\u3002\u7136\u800c\uff0c\u5b83\u7684\u52a3\u52bf\u5728\u4e8e\u7f3a\u4e4f\u5185\u7f6e\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u7528\u6237\u8ba4\u8bc1\u548c\u7ba1\u7406\u529f\u80fd\uff0c\u9700\u8981\u5f00\u53d1\u8005\u81ea\u884c\u5b9e\u73b0\u6216\u5f15\u5165\u6269\u5c55\u5e93\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0cFlask\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u914d\u7f6e\u548c\u7ba1\u7406\uff0c\u589e\u52a0\u4e86\u5f00\u53d1\u590d\u6742\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PYTHON\u5982\u4f55\u4f7f\u7528FLASK \u5728Python\u4e2d\uff0cFlask\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\u3002\u5b83\u9002\u7528\u4e8e [&hellip;]","protected":false},"author":3,"featured_media":940491,"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\/940489"}],"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=940489"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940489\/revisions"}],"predecessor-version":[{"id":940492,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940489\/revisions\/940492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/940491"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=940489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=940489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=940489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}