{"id":103,"date":"2022-01-22T13:00:30","date_gmt":"2022-01-22T05:00:30","guid":{"rendered":"https:\/\/lmceric.top\/?p=103"},"modified":"2022-01-22T13:00:31","modified_gmt":"2022-01-22T05:00:31","slug":"python-flask%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/","title":{"rendered":"python flask\u5165\u95e8"},"content":{"rendered":"\n<p>\u672c\u6587\u662f\u4f5c\u8005\u5728\u5f00\u53d1api\u7f51\u5173\u8fc7\u7a0b\u4e2d\u7684\u5b66\u4e60\u6210\u679c\uff0c\u4ee5\u6b64\u8bb0\u5f55<\/p>\n\n\n\n<p>\u5728\u9605\u8bfb\u672c\u6587\u4e4b\u524d\uff0c\u6211\u5047\u8bbe\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86 Python \u548c HTML \u7684\u57fa\u7840\u77e5\u8bc6\u3002\u5982\u679c\u8fd8\u6ca1\u6709\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u53bb\u5404\u5927\u6559\u7a0b\u5e73\u53f0\u81ea\u884c\u4e86\u89e3<\/p>\n\n\n\n<p>\u672c\u6587\u73af\u5883\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>\u73af\u5883<\/th><th>\u7248\u672c<\/th><\/tr><\/thead><tbody><tr><td>\u64cd\u4f5c\u7cfb\u7edf<\/td><td>windows10<\/td><\/tr><tr><td>python<\/td><td>3.9.5<\/td><\/tr><tr><td>IDE<\/td><td>pycharm community<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u9884\u5907\u77e5\u8bc6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 Flask<\/h3>\n\n\n\n<p>\u8ffd\u6eaf\u5230\u6700\u521d\uff0cFlask \u8bde\u751f\u4e8e Armin Ronacher \u5728 2010 \u5e74\u611a\u4eba\u8282\u5f00\u7684\u4e00\u4e2a\u73a9\u7b11\u3002\u540e\u6765\uff0c\u5b83\u9010\u6e10\u53d1\u5c55\u6210\u4e3a\u4e00\u4e2a\u6210\u719f\u7684 Python Web \u6846\u67b6\uff0c\u8d8a\u6765\u8d8a\u53d7\u5230\u5f00\u53d1\u8005\u7684\u559c\u7231\u3002\u76ee\u524d\u5b83\u5728 GitHub \u4e0a\u662f Star \u6570\u91cf\u6700\u591a\u7684 Python Web \u6846\u67b6\uff0c\u6ca1\u6709\u4e4b\u4e00\u3002<\/p>\n\n\n\n<p>Flask \u662f\u5178\u578b\u7684\u5fae\u6846\u67b6\uff0c\u4f5c\u4e3a Web \u6846\u67b6\u6765\u8bf4\uff0c\u5b83\u4ec5\u4fdd\u7559\u4e86\u6838\u5fc3\u529f\u80fd\uff1a<strong>\u8bf7\u6c42\u54cd\u5e94\u5904\u7406<\/strong>\u548c<strong>\u6a21\u677f\u6e32\u67d3<\/strong>\u3002\u8fd9\u4e24\u7c7b\u529f\u80fd\u5206\u522b\u7531 Werkzeug\uff08WSGI \u5de5\u5177\u5e93\uff09\u5b8c\u6210\u548c Jinja\uff08\u6a21\u677f\u6e32\u67d3\u5e93\uff09\u5b8c\u6210\uff0c\u56e0\u4e3a Flask \u5305\u88c5\u4e86\u8fd9\u4e24\u4e2a\u4f9d\u8d56\uff0c\u6211\u4eec\u6682\u65f6\u4e0d\u7528\u6df1\u5165\u4e86\u89e3\u5b83\u4eec\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 WSGI<\/h3>\n\n\n\n<p>Web Server Gateway Interface\uff08Web\u670d\u52a1\u5668\u7f51\u5173\u63a5\u53e3\uff0cWSGI\uff09\u5df2\u88ab\u7528\u4f5cPython Web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6807\u51c6\u3002 WSGI\u662fWeb\u670d\u52a1\u5668\u548cWeb\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u901a\u7528\u63a5\u53e3\u7684\u89c4\u8303\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 Werkzeug<\/h3>\n\n\n\n<p>\u5b83\u662f\u4e00\u4e2aWSGI\u5de5\u5177\u5305\uff0c\u5b83\u5b9e\u73b0\u4e86\u8bf7\u6c42\uff0c\u54cd\u5e94\u5bf9\u8c61\u548c\u5b9e\u7528\u51fd\u6570\u3002 \u8fd9\u4f7f\u5f97\u80fd\u591f\u5728\u5176\u4e0a\u6784\u5efaweb\u6846\u67b6\u3002 Flask\u6846\u67b6\u4f7f\u7528Werkzeug\u4f5c\u4e3a\u5176\u57fa\u7840\u4e4b\u4e00\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.4 jinja2<\/h3>\n\n\n\n<p>jinja2\u662fPython\u7684\u4e00\u4e2a\u6d41\u884c\u7684\u6a21\u677f\u5f15\u64ce\u3002Web\u6a21\u677f\u7cfb\u7edf\u5c06\u6a21\u677f\u4e0e\u7279\u5b9a\u6570\u636e\u6e90\u7ec4\u5408\u4ee5\u5448\u73b0\u52a8\u6001\u7f51\u9875\u3002<\/p>\n\n\n\n<p>Flask\u901a\u5e38\u88ab\u79f0\u4e3a\u5fae\u6846\u67b6\u3002 \u5b83\u65e8\u5728\u4fdd\u6301\u5e94\u7528\u7a0b\u5e8f\u7684\u6838\u5fc3\u7b80\u5355\u4e14\u53ef\u6269\u5c55\u3002Flask\u6ca1\u6709\u7528\u4e8e\u6570\u636e\u5e93\u5904\u7406\u7684\u5185\u7f6e\u62bd\u8c61\u5c42\uff0c\u4e5f\u6ca1\u6709\u5f62\u6210\u9a8c\u8bc1\u652f\u6301\u3002\u76f8\u53cd\uff0cFlask\u652f\u6301\u6269\u5c55\u4ee5\u5411\u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u6b64\u7c7b\u529f\u80fd\u3002\u4e00\u4e9b\u53d7\u6b22\u8fce\u7684Flask\u6269\u5c55\u5c06\u5728\u672c\u6559\u7a0b\u540e\u7eed\u7ae0\u8282\u8fdb\u884c\u8ba8\u8bba\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u73af\u5883<\/h2>\n\n\n\n<p><strong>virtualenv<\/strong>\u662f\u4e00\u4e2a\u865a\u62df\u7684Python\u73af\u5883\u6784\u5efa\u5668\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u5e76\u884c\u521b\u5efa\u591a\u4e2aPython\u73af\u5883\u3002 \u56e0\u6b64\uff0c\u5b83\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u7248\u672c\u7684\u5e93\u4e4b\u95f4\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u4e0b\u5217\u547d\u4ee4\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install virtualenv<\/pre>\n\n\n\n<p>\u6b64\u547d\u4ee4\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u60a8\u53ef\u4ee5\u5728Linux \/ Mac OS\u4e0a\u7684 <strong>pip<\/strong> \u4e4b\u524d\u6dfb\u52a0 <strong>sudo<\/strong> \u3002<\/p>\n\n\n\n<p>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fWindows\uff0c\u8bf7\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u767b\u5f55\u3002\u5728Ubuntu\u4e0a\uff0c <strong>virtualenv<\/strong>\u53ef\u4ee5\u4f7f\u7528\u5b83\u7684\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install virtualenv<\/pre>\n\n\n\n<p>\u5b89\u88c5\u540e\uff0c\u5c06\u5728\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u65b0\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir newproj<br>cd newproj<br>virtualenv venv<\/pre>\n\n\n\n<p>\u8981\u5728 <strong>Linux \/ OS X<\/strong> \u4e0a\u6fc0\u6d3b\u76f8\u5e94\u7684\u73af\u5883\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">venv\/bin\/activate<\/pre>\n\n\n\n<p>\u8981\u5728 <strong>Windows<\/strong> \u4e0a\u6fc0\u6d3b\u76f8\u5e94\u7684\u73af\u5883\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">venv\\scripts\\activate<\/pre>\n\n\n\n<p>\u6211\u4eec\u73b0\u5728\u51c6\u5907\u5728\u8fd9\u4e2a\u73af\u5883\u4e2d\u5b89\u88c5Flask\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install Flask<\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u547d\u4ee4\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\uff0c\u4e0d\u9700\u8981\u7cfb\u7edf\u8303\u56f4\u5b89\u88c5\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.Hello Flask<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u7b2c\u4e00\u4e2aapp<\/h3>\n\n\n\n<p>\u4e3a\u4e86\u6d4b\u8bd5 <strong>Flask<\/strong> \u5b89\u88c5\uff0c\u8bf7\u5728\u7f16\u8f91\u5668\u4e2d\u5c06\u4ee5\u4e0b\u4ee3\u7801\u8f93\u5165 <strong>Hello.py\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>app = Flask(__name__)<br>\u200b<br>@app.route('\/')<br>def hello_world():<br> &nbsp; return 'Hello World'<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; app.run()<\/pre>\n\n\n\n<p>\u5fc5\u987b\u5728\u9879\u76ee\u4e2d\u5bfc\u5165Flask\u6a21\u5757\u3002<\/p>\n\n\n\n<p>Flask\u7c7b\u7684\u4e00\u4e2a\u5bf9\u8c61\u662f\u6211\u4eec\u7684<strong>WSGI<\/strong>\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>Flask\u6784\u9020\u51fd\u6570\u4f7f\u7528<strong>\u5f53\u524d\u6a21\u5757\uff08name \uff09<\/strong>\u7684\u540d\u79f0\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n\n\n\n<p>Flask\u7c7b\u7684<strong>route()<\/strong>\u51fd\u6570\u662f\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u5b83\u544a\u8bc9\u5e94\u7528\u7a0b\u5e8f\u54ea\u4e2aURL\u5e94\u8be5\u8c03\u7528\u76f8\u5173\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">app.route(rule, options)<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>rule<\/strong> \u53c2\u6570\u8868\u793a\u4e0e\u8be5\u51fd\u6570\u7684URL\u7ed1\u5b9a\u3002<\/li><li><strong>options<\/strong> \u662f\u8981\u8f6c\u53d1\u7ed9\u57fa\u7840Rule\u5bf9\u8c61\u7684\u53c2\u6570\u5217\u8868\u3002<\/li><\/ul>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c&#8217;\/ &#8216; URL\u4e0e<strong>hello_world()<\/strong>\u51fd\u6570\u7ed1\u5b9a\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5f53\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00web\u670d\u52a1\u5668\u7684\u4e3b\u9875\u65f6\uff0c\u5c06\u5448\u73b0\u8be5\u51fd\u6570\u7684\u8f93\u51fa\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0cFlask\u7c7b\u7684<strong>run()<\/strong>\u65b9\u6cd5\u5728\u672c\u5730\u5f00\u53d1\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">app.run(host, port, debug, options)<\/pre>\n\n\n\n<p>\u6240\u6709\u53c2\u6570\u90fd\u662f\u53ef\u9009\u7684<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>\u5e8f\u53f7<\/th><th>\u53c2\u6570\u4e0e\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td><strong>host<\/strong> \u8981\u76d1\u542c\u7684\u4e3b\u673a\u540d\u3002 \u9ed8\u8ba4\u4e3a127.0.0.1\uff08localhost\uff09\u3002\u8bbe\u7f6e\u4e3a\u201c0.0.0.0\u201d\u4ee5\u4f7f\u670d\u52a1\u5668\u5728\u5916\u90e8\u53ef\u7528<\/td><\/tr><tr><td>2<\/td><td><strong>port<\/strong> \u9ed8\u8ba4\u503c\u4e3a5000<\/td><\/tr><tr><td>3<\/td><td><strong>debug<\/strong> \u9ed8\u8ba4\u4e3afalse\u3002 \u5982\u679c\u8bbe\u7f6e\u4e3atrue\uff0c\u5219\u63d0\u4f9b\u8c03\u8bd5\u4fe1\u606f<\/td><\/tr><tr><td>4<\/td><td><strong>options<\/strong> \u8981\u8f6c\u53d1\u5230\u5e95\u5c42\u7684Werkzeug\u670d\u52a1\u5668\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e0a\u9762\u7ed9\u51fa\u7684<strong>Python<\/strong>\u811a\u672c\u662f\u4ecePython shell\u6267\u884c\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python Hello.py<\/pre>\n\n\n\n<p>Python shell\u4e2d\u7684\u6d88\u606f\u901a\u77e5\u60a8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">* Running on http:\/\/127.0.0.1:5000\/ (Press CTRL+C to quit)<\/pre>\n\n\n\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u4e0a\u8ff0URL<strong>\uff08localhost\uff1a5000\uff09<\/strong>\u3002\u5c06\u663e\u793a<strong>\u201cHello World\u201d<\/strong>\u6d88\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u54cd\u5e94GET\u8bf7\u6c42<\/h3>\n\n\n\n<p>Http\u534f\u8bae\u662f\u4e07\u7ef4\u7f51\u4e2d\u6570\u636e\u901a\u4fe1\u7684\u57fa\u7840\u3002\u5728\u8be5\u534f\u8bae\u4e2d\u5b9a\u4e49\u4e86\u4ece\u6307\u5b9aURL\u68c0\u7d22\u6570\u636e\u7684\u4e0d\u540c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e0b\u8868\u603b\u7ed3\u4e86\u4e0d\u540c\u7684http\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>\u5e8f\u53f7<\/th><th>\u65b9\u6cd5\u4e0e\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td><strong>GET<\/strong>\u4ee5\u672a\u52a0\u5bc6\u7684\u5f62\u5f0f\u5c06\u6570\u636e\u53d1\u9001\u5230\u670d\u52a1\u5668\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/td><\/tr><tr><td>2<\/td><td><strong>HEAD<\/strong>\u548cGET\u65b9\u6cd5\u76f8\u540c\uff0c\u4f46\u6ca1\u6709\u54cd\u5e94\u4f53\u3002<\/td><\/tr><tr><td>3<\/td><td><strong>POST<\/strong>\u7528\u4e8e\u5c06HTML\u8868\u5355\u6570\u636e\u53d1\u9001\u5230\u670d\u52a1\u5668\u3002POST\u65b9\u6cd5\u63a5\u6536\u7684\u6570\u636e\u4e0d\u7531\u670d\u52a1\u5668\u7f13\u5b58\u3002<\/td><\/tr><tr><td>4<\/td><td><strong>PUT<\/strong>\u7528\u4e0a\u4f20\u7684\u5185\u5bb9\u66ff\u6362\u76ee\u6807\u8d44\u6e90\u7684\u6240\u6709\u5f53\u524d\u8868\u793a\u3002<\/td><\/tr><tr><td>5<\/td><td><strong>DELETE<\/strong> \u5220\u9664\u7531URL\u7ed9\u51fa\u7684\u76ee\u6807\u8d44\u6e90\u7684\u6240\u6709\u5f53\u524d\u8868\u793a\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cFlask\u8def\u7531\u54cd\u5e94<strong>GET<\/strong>\u8bf7\u6c42\u3002\u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e3a<strong>route()<\/strong>\u88c5\u9970\u5668\u63d0\u4f9b\u65b9\u6cd5\u53c2\u6570\u6765\u66f4\u6539\u6b64\u9996\u9009\u9879\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u5728Python shell\u4e2d\u8f93\u5165\u4ee5\u4e0b\u811a\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask, redirect, url_for, request<br>app = Flask(__name__)<br>\u200b<br>@app.route('\/success\/&lt;name&gt;')<br>def success(name):<br> &nbsp; return 'welcome %s' % name<br>\u200b<br>@app.route('\/login',methods = ['GET'])<br>def login():<br> &nbsp; &nbsp; &nbsp;user = request.args.get('name')<br> &nbsp; &nbsp; &nbsp;return redirect(url_for('success',name = user))<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; app.run(debug = True)<\/pre>\n\n\n\n<p>\u8fd9\u65f6\uff0c\u5411\u5730\u5740<code>http:\/\/localhost:5000\/login<\/code>\u53d1\u9001\u5305\u542b\u53c2\u6570<code>name=test<\/code>\u7684 <code>GET<\/code>\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u5c06\u8fd4\u56de&#8221;wlecome test&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u8fd4\u56dehtml\u7f51\u9875<\/h3>\n\n\n\n<p>flask\u8fd4\u56dehtml\u7f51\u9875\u5b9e\u9645\u4e0a\u662f\u57fa\u4e8e\u54cd\u5e94GET\u8bf7\u6c42\uff0c\u8fd4\u56dehtml\u9875\u9762<\/p>\n\n\n\n<p><strong>\u89c6\u56fe\u51fd\u6570\u6709\u4e24\u4e2a\u4f5c\u7528:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5904\u7406\u4e1a\u52a1\u903b\u8f91<\/li><li>\u8fd4\u56de\u54cd\u5e94\u5185\u5bb9<\/li><\/ul>\n\n\n\n<p>\u5728\u5927\u578b\u5e94\u7528\u4e2d,\u628a\u4e1a\u52a1\u903b\u8f91\u548c\u8868\u73b0\u5185\u5bb9\u653e\u5728\u4e00\u8d77,\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u548c\u7ef4\u62a4\u6210\u672c.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6a21\u677f\u5176\u5b9e\u662f\u4e00\u4e2a\u5305\u542b\u54cd\u5e94\u6587\u672c\u7684\u6587\u4ef6,\u5176\u4e2d\u7528\u5360\u4f4d\u7b26(\u53d8\u91cf)\u8868\u793a\u52a8\u6001\u90e8\u5206,\u544a\u8bc9\u6a21\u677f\u5f15\u64ce\u5176\u5177\u4f53\u7684\u503c\u9700\u8981\u4ece\u4f7f\u7528\u7684\u6570\u636e\u4e2d\u83b7\u53d6<\/li><li>\u4f7f\u7528\u771f\u5b9e\u503c\u66ff\u6362\u53d8\u91cf,\u518d\u8fd4\u56de\u6700\u7ec8\u5f97\u5230\u7684\u5b57\u7b26\u4e32,\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a&#8217;\u6e32\u67d3&#8217;<\/li><li>Flask \u662f\u4f7f\u7528 Jinja2 \u8fd9\u4e2a\u6a21\u677f\u5f15\u64ce\u6765\u6e32\u67d3\u6a21\u677f<\/li><\/ul>\n\n\n\n<p><strong>\u4f7f\u7528\u6a21\u677f\u7684\u597d\u5904<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u89c6\u56fe\u51fd\u6570\u53ea\u8d1f\u8d23\u4e1a\u52a1\u903b\u8f91\u548c\u6570\u636e\u5904\u7406(\u4e1a\u52a1\u903b\u8f91\u65b9\u9762)<\/li><li>\u800c\u6a21\u677f\u5219\u53d6\u5230\u89c6\u56fe\u51fd\u6570\u7684\u6570\u636e\u7ed3\u679c\u8fdb\u884c\u5c55\u793a(\u89c6\u56fe\u5c55\u793a\u65b9\u9762)<\/li><li>\u4ee3\u7801\u7ed3\u6784\u6e05\u6670,\u8026\u5408\u5ea6\u4f4e\u6a21\u677f\u57fa\u672c\u4f7f\u7528<\/li><\/ul>\n\n\n\n<p>\u5728\u9879\u76ee\u4e0b\u521b\u5efa templates \u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653e\u6240\u6709\u6a21\u677f\u6587\u4ef6\uff0c\u5e76\u5728\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6 html \u6587\u4ef6 hello.html<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;<br>&lt;html lang=\"en\"&gt;<br>&lt;head&gt;<br> &nbsp; &nbsp;&lt;meta charset=\"UTF-8\"&gt;<br> &nbsp; &nbsp;&lt;title&gt;Title&lt;\/title&gt;<br>&lt;\/head&gt;<br>&lt;body&gt;<br>\u6211\u7684\u6a21\u677fhtml\u5185\u5bb9<br>&lt;\/body&gt;<br>&lt;\/html&gt;<\/pre>\n\n\n\n<p>\u521b\u5efa\u89c6\u56fe\u51fd\u6570,\u5c06\u8be5\u6a21\u677f\u5185\u5bb9\u8fdb\u884c\u6e32\u67d3\u8fd4\u56de<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask, render_template<br>\u200b<br>app = Flask(__name__)<br>\u200b<br>@app.route('\/')<br>def index():<br> &nbsp; &nbsp;return render_template('hello.html')<\/pre>\n\n\n\n<p><strong>\u6a21\u677f\u53d8\u91cf<\/strong><\/p>\n\n\n\n<p>\u4ee3\u7801\u4e2d\u4f20\u5165\u5b57\u7b26\u4e32\uff0c\u5217\u8868\uff0c\u5b57\u5178\u5230\u6a21\u677f\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask, render_template<br>\u200b<br>app = Flask(__name__)<br>\u200b<br>@app.route('\/')<br>def index():<br> &nbsp; &nbsp;# \u5f80\u6a21\u677f\u4e2d\u4f20\u5165\u7684\u6570\u636e<br> &nbsp; &nbsp;my_str = 'Hello Word'<br> &nbsp; &nbsp;my_int = 10<br> &nbsp; &nbsp;my_array = [3, 4, 2, 1, 7, 9]<br> &nbsp; &nbsp;my_dict = {<br> &nbsp; &nbsp; &nbsp; &nbsp;'name': 'xiaoming',<br> &nbsp; &nbsp; &nbsp; &nbsp;'age': 18<br> &nbsp;  }<br> &nbsp; &nbsp;return render_template('hello.html',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my_str=my_str,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my_int=my_int,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my_array=my_array,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my_dict=my_dict<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )<\/pre>\n\n\n\n<p>\u6a21\u677f\u4e2d\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;<br>&lt;html lang=\"en\"&gt;<br>&lt;head&gt;<br> &nbsp;&lt;meta charset=\"UTF-8\"&gt;<br> &nbsp;&lt;title&gt;Title&lt;\/title&gt;<br>&lt;\/head&gt;<br>&lt;body&gt;<br>  \u6211\u7684\u6a21\u677fhtml\u5185\u5bb9<br> &nbsp;&lt;br \/&gt;{{ my_str }}<br> &nbsp;&lt;br \/&gt;{{ my_int }}<br> &nbsp;&lt;br \/&gt;{{ my_array }}<br> &nbsp;&lt;br \/&gt;{{ my_dict }}<br>&lt;\/body&gt;<br>&lt;\/html&gt;<\/pre>\n\n\n\n<p>\u8fd0\u884c\u6548\u679c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6211\u7684\u6a21\u677fhtml\u5185\u5bb9<br>Hello Word<br>10<br>[3, 4, 2, 1, 7, 9]<br>{'name': 'xiaoming', 'age': 18}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u662f\u4f5c\u8005\u5728\u5f00\u53d1api\u7f51\u5173\u8fc7\u7a0b\u4e2d\u7684\u5b66\u4e60\u6210\u679c\uff0c\u4ee5\u6b64\u8bb0&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/\">Continue reading<span class=\"screen-reader-text\">python flask\u5165\u95e8<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[11],"tags":[27,6],"class_list":["post-103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tec","tag-python","tag-6","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>python flask\u5165\u95e8 - LMC\u7684\u4e2a\u4eba\u7ad9<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python flask\u5165\u95e8 - LMC\u7684\u4e2a\u4eba\u7ad9\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6587\u662f\u4f5c\u8005\u5728\u5f00\u53d1api\u7f51\u5173\u8fc7\u7a0b\u4e2d\u7684\u5b66\u4e60\u6210\u679c\uff0c\u4ee5\u6b64\u8bb0&#8230;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/\" \/>\n<meta property=\"og:site_name\" content=\"LMC\u7684\u4e2a\u4eba\u7ad9\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-22T05:00:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-22T05:00:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/01\/v2-bf16024f191c862d9b246e07df23e055_r.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"900\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"SimpleAstronaut\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"SimpleAstronaut\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/\"},\"author\":{\"name\":\"SimpleAstronaut\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/#\\\/schema\\\/person\\\/1edca9cfe5ff17dbe947c332a66ee539\"},\"headline\":\"python flask\u5165\u95e8\",\"datePublished\":\"2022-01-22T05:00:30+00:00\",\"dateModified\":\"2022-01-22T05:00:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/\"},\"wordCount\":143,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/lmceric.top\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/v2-bf16024f191c862d9b246e07df23e055_r.jpg\",\"keywords\":[\"python\",\"\u4e92\u8054\u7f51\"],\"articleSection\":[\"\u6280\u672f\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/\",\"url\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/\",\"name\":\"python flask\u5165\u95e8 - LMC\u7684\u4e2a\u4eba\u7ad9\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/lmceric.top\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/v2-bf16024f191c862d9b246e07df23e055_r.jpg\",\"datePublished\":\"2022-01-22T05:00:30+00:00\",\"dateModified\":\"2022-01-22T05:00:31+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/#\\\/schema\\\/person\\\/1edca9cfe5ff17dbe947c332a66ee539\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/#primaryimage\",\"url\":\"https:\\\/\\\/lmceric.top\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/v2-bf16024f191c862d9b246e07df23e055_r.jpg\",\"contentUrl\":\"https:\\\/\\\/lmceric.top\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/v2-bf16024f191c862d9b246e07df23e055_r.jpg\",\"width\":1920,\"height\":900},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/01\\\/22\\\/103\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/lmceric.top\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python flask\u5165\u95e8\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/#website\",\"url\":\"https:\\\/\\\/lmceric.top\\\/\",\"name\":\"LMC\u7684\u4e2a\u4eba\u7ad9\",\"description\":\"\u5b64\u5e06\u8fdc\u5f71\u78a7\u7a7a\u5c3d\uff0c\u552f\u89c1\u957f\u6c5f\u5929\u9645\u6d41\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/lmceric.top\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/#\\\/schema\\\/person\\\/1edca9cfe5ff17dbe947c332a66ee539\",\"name\":\"SimpleAstronaut\",\"sameAs\":[\"http:\\\/\\\/lmceric.top\"],\"url\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/author\\\/simpleastronaut\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"python flask\u5165\u95e8 - LMC\u7684\u4e2a\u4eba\u7ad9","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/","og_locale":"zh_CN","og_type":"article","og_title":"python flask\u5165\u95e8 - LMC\u7684\u4e2a\u4eba\u7ad9","og_description":"\u672c\u6587\u662f\u4f5c\u8005\u5728\u5f00\u53d1api\u7f51\u5173\u8fc7\u7a0b\u4e2d\u7684\u5b66\u4e60\u6210\u679c\uff0c\u4ee5\u6b64\u8bb0&#8230;","og_url":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/","og_site_name":"LMC\u7684\u4e2a\u4eba\u7ad9","article_published_time":"2022-01-22T05:00:30+00:00","article_modified_time":"2022-01-22T05:00:31+00:00","og_image":[{"width":1920,"height":900,"url":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/01\/v2-bf16024f191c862d9b246e07df23e055_r.jpg","type":"image\/jpeg"}],"author":"SimpleAstronaut","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"SimpleAstronaut","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/#article","isPartOf":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/"},"author":{"name":"SimpleAstronaut","@id":"https:\/\/lmceric.top\/#\/schema\/person\/1edca9cfe5ff17dbe947c332a66ee539"},"headline":"python flask\u5165\u95e8","datePublished":"2022-01-22T05:00:30+00:00","dateModified":"2022-01-22T05:00:31+00:00","mainEntityOfPage":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/"},"wordCount":143,"commentCount":0,"image":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/#primaryimage"},"thumbnailUrl":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/01\/v2-bf16024f191c862d9b246e07df23e055_r.jpg","keywords":["python","\u4e92\u8054\u7f51"],"articleSection":["\u6280\u672f"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/","url":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/","name":"python flask\u5165\u95e8 - LMC\u7684\u4e2a\u4eba\u7ad9","isPartOf":{"@id":"https:\/\/lmceric.top\/#website"},"primaryImageOfPage":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/#primaryimage"},"image":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/#primaryimage"},"thumbnailUrl":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/01\/v2-bf16024f191c862d9b246e07df23e055_r.jpg","datePublished":"2022-01-22T05:00:30+00:00","dateModified":"2022-01-22T05:00:31+00:00","author":{"@id":"https:\/\/lmceric.top\/#\/schema\/person\/1edca9cfe5ff17dbe947c332a66ee539"},"breadcrumb":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/#primaryimage","url":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/01\/v2-bf16024f191c862d9b246e07df23e055_r.jpg","contentUrl":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/01\/v2-bf16024f191c862d9b246e07df23e055_r.jpg","width":1920,"height":900},{"@type":"BreadcrumbList","@id":"https:\/\/lmceric.top\/index.php\/2022\/01\/22\/103\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/lmceric.top\/"},{"@type":"ListItem","position":2,"name":"python flask\u5165\u95e8"}]},{"@type":"WebSite","@id":"https:\/\/lmceric.top\/#website","url":"https:\/\/lmceric.top\/","name":"LMC\u7684\u4e2a\u4eba\u7ad9","description":"\u5b64\u5e06\u8fdc\u5f71\u78a7\u7a7a\u5c3d\uff0c\u552f\u89c1\u957f\u6c5f\u5929\u9645\u6d41","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/lmceric.top\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/lmceric.top\/#\/schema\/person\/1edca9cfe5ff17dbe947c332a66ee539","name":"SimpleAstronaut","sameAs":["http:\/\/lmceric.top"],"url":"https:\/\/lmceric.top\/index.php\/author\/simpleastronaut\/"}]}},"jetpack_featured_media_url":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/01\/v2-bf16024f191c862d9b246e07df23e055_r.jpg","_links":{"self":[{"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":1,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":105,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/posts\/103\/revisions\/105"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/media\/104"}],"wp:attachment":[{"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}