{"id":1029,"date":"2022-06-15T15:17:29","date_gmt":"2022-06-15T07:17:29","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=1029"},"modified":"2022-06-15T15:17:29","modified_gmt":"2022-06-15T07:17:29","slug":"python%e7%94%a8flask%e5%bb%ba%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%bd%91%e7%ab%99","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2022\/06\/15\/python%e7%94%a8flask%e5%bb%ba%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%bd%91%e7%ab%99\/","title":{"rendered":"python\u7528flask\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7f51\u7ad9"},"content":{"rendered":"\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u8bed\u8a00\u5feb\u901f\u5b9e\u73b0\u4e00\u4e2a\u7f51\u7ad9\u6216Web\u670d\u52a1\u3002\u672c\u6587\u53c2\u8003\u81ea<a rel=\"noreferrer noopener\" href=\"http:\/\/flask.pocoo.org\/docs\/0.12\/\" target=\"_blank\">Flask\u5b98\u65b9\u6587\u6863<\/a>\uff0c\u5927\u90e8\u5206\u4ee3\u7801\u5f15\u7528\u81ea\u5b98\u65b9\u6587\u6863\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u6211\u4eec\u6765\u5b89\u88c5Flask\u3002\u6700\u7b80\u5355\u7684\u529e\u6cd5\u5c31\u662f\u4f7f\u7528pip\uff0c\u5f53\u4f60\u88c5\u597d\u4e86python\u540e\uff0c\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u4e0b\u8f93\u5165\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install flask<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u8f93\u5165\u4e0b\u9762\u7684\u5185\u5bb9\u5e76\u4fdd\u5b58\u4e3aapp.py\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e0b\u6362\u5230\u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff0c\u518d(python app.py)\u8fd0\u884c\u8be5\u6587\u4ef6\u3002\u7136\u540e\u6d4f\u89c8\u5668\u8bbf\u95ee<code>IP:5000<\/code>\uff0c\u6211\u4eec\u5e94\u5f53\u53ef\u4ee5\u770b\u5230\u6d4f\u89c8\u5668\u4e0a\u8f93\u51fa\u4e86hello world\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask\napp = Flask(__name__)\n\n@app.route('\/')\ndef hello_world():\n    return 'hello world'\n\nif __name__ == '__main__':\n    app.run(host='0.0.0.0',port=5000)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u8def\u5f84\u53d8\u91cf\">\u8def\u5f84\u53d8\u91cf<\/h3>\n\n\n\n<p>\u5982\u679c\u5e0c\u671b\u83b7\u53d6<code>\/article\/1<\/code>\u8fd9\u6837\u7684\u8def\u5f84\u53c2\u6570\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u8def\u5f84\u53d8\u91cf\u3002\u8def\u5f84\u53d8\u91cf\u7684\u8bed\u6cd5\u662f<code>\/path\/&lt;converter:varname&gt;<\/code>\u3002\u5728\u8def\u5f84\u53d8\u91cf\u524d\u8fd8\u53ef\u4ee5\u4f7f\u7528\u53ef\u9009\u7684\u8f6c\u6362\u5668\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u8f6c\u6362\u5668\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u8f6c\u6362\u5668<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td>string<\/td><td>\u9ed8\u8ba4\u9009\u9879\uff0c\u63a5\u53d7\u9664\u4e86\u659c\u6760\u4e4b\u5916\u7684\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>int<\/td><td>\u63a5\u53d7\u6574\u6570<\/td><\/tr><tr><td>float<\/td><td>\u63a5\u53d7\u6d6e\u70b9\u6570<\/td><\/tr><tr><td>path<\/td><td>\u548cstring\u7c7b\u4f3c\uff0c\u4e0d\u8fc7\u53ef\u4ee5\u63a5\u53d7\u5e26\u659c\u6760\u7684\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>any<\/td><td>\u5339\u914d\u4efb\u4f55\u4e00\u79cd\u8f6c\u6362\u5668<\/td><\/tr><tr><td>uuid<\/td><td>\u63a5\u53d7UUID\u5b57\u7b26\u4e32<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e0b\u9762\u662fFlask\u5b98\u65b9\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@app.route('\/user\/&lt;username&gt;')\ndef show_user_profile(username):\n    # show the user profile for that user\n    return 'User %s' % username\n\n@app.route('\/post\/&lt;int:post_id&gt;')\ndef show_post(post_id):\n    # show the post with the given id, the id is an integer\n    return 'Post %d' % post_id<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6784\u9020url\">\u6784\u9020URL<\/h3>\n\n\n\n<p>\u901a\u8fc7\u6784\u5efaURL\u7684\u65b9\u5f0f\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u62fcURL\u7684\u539f\u56e0\u6709\u4e24\u70b9\uff1a<br>1.\u5c06\u6765\u5982\u679c\u4fee\u6539\u4e86URL,\u4f46\u6ca1\u6709\u4fee\u6539\u8be5URL\u5bf9\u5e94\u7684\u51fd\u6570\u540d\uff0c\u5c31\u4e0d\u7528\u5230\u5904\u53bb\u66ff\u6362URL\u4e86\u3002<br>2. url_for\uff08\uff09\u51fd\u6570\u4f1a\u8f6c\u4e49\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u548cunicode\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e9b\u4e8b\u60c5url_for\u4f1a\u81ea\u52a8\u7684\u5e2e\u52a9\u6211\u4eec\u641e\u5b9a\u3002<\/p>\n\n\n\n<p>\u5728Web\u7a0b\u5e8f\u4e2d\u5e38\u5e38\u9700\u8981\u83b7\u53d6\u67d0\u4e2a\u9875\u9762\u7684URL\uff0c\u5728Flask\u4e2d\u9700\u8981\u4f7f\u7528<code>url_for('\u65b9\u6cd5\u540d')<\/code>\u6765\u6784\u9020\u5bf9\u5e94\u65b9\u6cd5\u7684URL\u3002\u4e0b\u9762\u662fFlask\u5b98\u65b9\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; from flask import Flask, url_for\n&gt;&gt;&gt; app = Flask(__name__)\n&gt;&gt;&gt; @app.route('\/')\n... def index(): pass\n...\n&gt;&gt;&gt; @app.route('\/login')\n... def login(): pass\n...\n&gt;&gt;&gt; @app.route('\/user\/&lt;username&gt;')\n... def profile(username): pass\n...\n&gt;&gt;&gt; with app.test_request_context():\n...  print url_for('index')\n...  print url_for('login')\n...  print url_for('login', next='\/')\n...  print url_for('profile', username='John Doe')\n...\n\/\n\/login\n\/login?next=\/\n\/user\/John%20Doe<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"http\u65b9\u6cd5\">HTTP\u65b9\u6cd5<\/h3>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u5904\u7406\u5177\u4f53\u7684HTTP\u65b9\u6cd5\uff0c\u5728Flask\u4e2d\u4e5f\u5f88\u5bb9\u6613\uff0c\u4f7f\u7528<code>route<\/code>\u88c5\u9970\u5668\u7684<code>methods<\/code>\u53c2\u6570\u8bbe\u7f6e\u5373\u53ef\u3002<\/p>\n\n\n\n<p>get\u548cpost \u8bf7\u6c42\u533a\u522b\uff1a<br>get\u662f\u5c06\u8bf7\u6c42\u5199\u5728url\u91cc\u7684\u3002<br>\u800cpost\u662f\u5199\u5728form\u8868\u5355\u4e2d\u7684\u3002<\/p>\n\n\n\n<p>\u5728route\u7684methods\u4e2d\uff0c\u5199\u597dpost\u65b9\u6cd5\uff0c\u5426\u5219\u9ed8\u8ba4\u662fget\uff0c\u4e00\u65e6\u8bf7\u6c42\u65b9\u5f0f\u662fpost\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import request\n\n@app.route('\/login', methods=['GET', 'POST'])\ndef login():\n    if request.method == 'POST':\n        do_the_login()\n    else:\n        show_the_login_form()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u9759\u6001\u6587\u4ef6\">\u9759\u6001\u6587\u4ef6<\/h3>\n\n\n\n<p>Web\u7a0b\u5e8f\u4e2d\u5e38\u5e38\u9700\u8981\u5904\u7406\u9759\u6001\u6587\u4ef6\uff0c\u5728Flask\u4e2d\u9700\u8981\u4f7f\u7528<code>url_for<\/code>\u51fd\u6570\u5e76\u6307\u5b9a<code>static<\/code>\u7aef\u70b9\u540d\u548c\u6587\u4ef6\u540d\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u9645\u7684\u6587\u4ef6\u5e94\u653e\u5728<code>static\/<\/code>\u6587\u4ef6\u5939\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">url_for('static', filename='style.css')<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6a21\u677f\u751f\u6210\">\u6a21\u677f\u751f\u6210<\/h3>\n\n\n\n<p>Flask\u9ed8\u8ba4\u4f7f\u7528Jinja2\u4f5c\u4e3a\u6a21\u677f\uff0cFlask\u4f1a\u81ea\u52a8\u914d\u7f6eJinja \u6a21\u677f\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u5176\u4ed6\u914d\u7f6e\u4e86\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6a21\u677f\u6587\u4ef6\u9700\u8981\u653e\u5728<code>templates<\/code>\u6587\u4ef6\u5939\u4e0b\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528 Jinja \u6a21\u677f\uff0c\u53ea\u9700\u8981\u5728python\u6587\u4ef6\u4e2d\u4f7f\u7528<code>render_template<\/code>\u51fd\u6570\u5e76\u4f20\u5165\u6a21\u677f\u6587\u4ef6\u540d\u548c\u53c2\u6570\u540d\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import render_template\n\n@app.route('\/hello\/')\n@app.route('\/hello\/&lt;name&gt;')\ndef hello(name=None):\n    return render_template('hello.html', name=name)<\/pre>\n\n\n\n<p>\u76f8\u5e94\u7684<code>templates<\/code>\u6587\u4ef6\u5939\u91cc\u7684HTML\u6a21\u677f\u6587\u4ef6\u5982\u4e0b\u3002\u7528\u4e24\u4e2a\u5927\u62ec\u53f7\u5199\u53d8\u91cf\uff0c\u7528\u5927\u62ec\u53f7\u52a0\u767e\u5206\u53f7\u5199\u8bed\u53e5\uff0c\u8bbf\u95ee\u65f6\u8f93\u5165IP:\u7aef\u53e3\/hello\/\u5c31\u53ef\u4ee5\u8bbf\u95ee<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!doctype html&gt;\n&lt;title&gt;Hello from Flask&lt;\/title&gt;\n{% if name %}\n  &lt;h1&gt;Hello {{ name }}!&lt;\/h1&gt;\n{% else %}\n  &lt;h1&gt;Hello, World!&lt;\/h1&gt;\n{% endif %}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u8bed\u8a00\u5feb\u901f\u5b9e\u73b0\u4e00\u4e2a\u7f51\u7ad9\u6216Web\u670d\u52a1\u3002\u672c\u6587\u53c2\u8003\u81eaFlask\u5b98\u65b9\u6587\u6863\uff0c\u5927\u90e8\u5206\u4ee3\u7801\u5f15\u7528\u81ea\u5b98\u65b9\u6587 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1029","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1029"}],"version-history":[{"count":0,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1029\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1029"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}