{"id":6664,"date":"2021-09-01T23:05:54","date_gmt":"2021-09-01T15:05:54","guid":{"rendered":"https:\/\/vip.fxxkpython.com\/?p=6664"},"modified":"2021-10-27T19:25:34","modified_gmt":"2021-10-27T11:25:34","slug":"%e4%bd%bf%e7%94%a8-python-%e4%bb%8e-0-%e5%bc%80%e5%8f%91%e5%b1%9e%e4%ba%8e%e4%bd%a0%e7%9a%84%e4%b8%aa%e4%ba%ba%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f-24-%e4%bd%bf%e7%94%a8%e5%a5%bd%e4%bd%a0%e7%9a%84","status":"publish","type":"post","link":"https:\/\/vip.fxxkpython.com\/?p=6664","title":{"rendered":"\u4f7f\u7528 Python \u4ece 0 \u5f00\u53d1\u5c5e\u4e8e\u4f60\u7684\u4e2a\u4eba\u7ba1\u7406\u7cfb\u7edf 24 | \u4f7f\u7528\u597d\u4f60\u7684\u84dd\u672c\uff0c\u7075\u6d3b\u914d\u7f6e"},"content":{"rendered":"\n<p>\u4e4b\u524d\u7ed9\u4f60\u5206\u4eab\u8fc7\u5173\u4e8e\u300c<a href=\"https:\/\/vip.fxxkpython.com\/?p=6375\">\u84dd\u672c<\/a>\u300d\u7684\u77e5\u8bc6\u70b9\uff1a\u4e00\u4e2a\u9879\u76ee\uff0c\u53ef\u4ee5\u62c6\u5206\u6210\u591a\u4e2a\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u6a21\u5757\u5316\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u84dd\u672c\u53ef\u4ee5\u7edf\u4e00\u7ba1\u7406\u8d44\u6e90\u548c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u4f60\u7684\u4e2a\u4eba\u7ba1\u7406\u7cfb\u7edf\uff0c\u80af\u5b9a\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684 py \u6a21\u5757\u5c31\u80fd\u641e\u5b9a\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u6700\u597d\u8981\u628a\u84dd\u672c\u4f7f\u7528\u8d77\u6765\uff0c\u8ba9\u9879\u76ee\u66f4\u52a0\u6e05\u6670\u4e00\u4e9b\uff0c\u63a5\u4e0b\u6765\u6211\u4f1a\u7ee7\u7eed\u7ed9\u4f60\u6f14\u793a\uff0c\u6570\u636e\u5efa\u6a21\u597d\u4e86\u4e4b\u540e\uff0c\u5982\u4f55\u628a\u84dd\u672c\u7528\u597d\uff0c\u4ee5\u53ca\u5982\u4f55\u8ba9\u914d\u7f6e\u6587\u4ef6\u5199\u7684\u7075\u6d3b\u8d77\u6765\u3002<\/p>\n\n\n\n<h2>\u521b\u5efa\u84dd\u672c<\/h2>\n\n\n\n<p>\u6839\u636e\u4f60\u7684\u9700\u6c42\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u84dd\u672c\uff0c\u6bd4\u5982\u73b0\u5728\u7ed9\u4f60\u6f14\u793a\u7684\u300c\u4efb\u52a1\u62c6\u5206\u300d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e24\u4e2a\u84dd\u672c\uff0c\u4e00\u4e2a\u662f\u300cauth\u300d\u84dd\u672c\uff0c\u7528\u6765\u7edf\u4e00\u64cd\u4f5c\u7528\u6237\u7684\u884c\u4e3a\uff0c\u7136\u540e\u518d\u521b\u5efa\u4e00\u4e2a\u84dd\u672c\uff0c\u7528\u6765\u7edf\u4e00\u64cd\u4f5c\u300c\u8ba1\u5212\u300d\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<p>\u7528\u6237\u84dd\u672c<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>auth<\/p><p>\u251c\u2500\u2500 <strong>init<\/strong>.py<\/p><p>\u251c\u2500\u2500 forms.py<\/p><p>\u2514\u2500\u2500 views.py<\/p><\/blockquote>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a package\uff0c\u8fd9\u4e2a package \u5c31\u662f\u4e00\u4e2a\u84dd\u672c\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u91cc\u9762\u53ea\u505a\u7528\u6237\u64cd\u4f5c\u7684\u884c\u4e3a\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5728\u8fd9\u91cc\u521b\u5efa static\uff0c tempaltes \u7684\u9759\u6001\u8d44\u6e90\u6587\u4ef6\uff0c\u7edf\u4e00\u7ba1\u7406\u6837\u5f0f\u3002<\/p>\n\n\n\n<p>\u540c\u6837\u7684\uff0c\u521b\u5efa todo \u84dd\u672c:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>todo<\/p><p>\u251c\u2500\u2500 <strong>init<\/strong>.py<\/p><p>\u251c\u2500\u2500 forms.py<\/p><p>\u2514\u2500\u2500 views.py<\/p><\/blockquote>\n\n\n\n<p>\u4e00\u4e2a\u9053\u7406\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u81ea\u5df1\u7684\u9700\u6c42\u521b\u5efa\u597d\u76f8\u5e94\u7684\u84dd\u672c\u3002<\/p>\n\n\n\n<h2>\u6ce8\u518c\u84dd\u672c<\/h2>\n\n\n\n<p>\u4e00\u822c\u5728\u4f60\u521b\u5efa package \u4e2d\u7684 <strong>init<\/strong>.py \u4e2d\uff0c\u5bfc\u5165 Flask \u4e2d\u7684 Blueprint \u8fdb\u884c\u6ce8\u518c\uff0c\u6bd4\u5982\u8fd9\u91cc\u7684 auth \u84dd\u672c\u6ce8\u518c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">\nfrom Flask import Blueprint\n\n# \u84dd\u672c\u521b\u5efa\nauth = Blueprint('auth', __name__)\n<\/pre><\/div>\n\n\n\n<p>\u800c\u5728\u8fd9\u4e4b\u4e0b\u53ef\u4ee5\u7edf\u4e00\u4e9b\u5173\u4e8e\u7528\u6237\u7684\u8def\u7531\uff0c\u6211\u4eec\u7edf\u4e00\u5199\u5230\u8be5\u84dd\u672c\u4e0b\u7684 views.py \u4e0b\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">from mysystem import app, db, login_manager\nfrom mysystem.auth import user\nfrom mysystem.auth.forms import SignupForm\nfrom mysystem.models import User\n\n\n@user.route('\/signup', method=['POST'])\ndef signup():\n    form = SignupForm()\n    if form.validate_on_submit():\n        ....\n        \n@user.route('\/login', method=['POST'])\ndef login():\n        ....\n<\/pre><\/div>\n\n\n\n<p>\u7136\u540e\u5728 init.py \u4e2d\u5bfc\u5165\u5b83\u4eec\u5c31\u597d\u4e86\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">from Flask import Blueprint\n\n# \u84dd\u672c\u521b\u5efa\nauth = Blueprint('auth', __name__)\n\nfrom . import views\n<\/pre><\/div>\n\n\n\n<p>\u90a3\u4e48\u5728\u8fd9\u91cc auth \u84dd\u672c\u91cc\u6240\u6709\u7684\u64cd\u4f5c\uff0c\u90fd\u53ea\u4e0e\u7528\u6237\u7684\u64cd\u4f5c\u6709\u5173\u3002\u7a0b\u5e8f\u5982\u4f55\u8bc6\u522b\u5b83\u4eec\u5462\uff1f<div style=\"text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;\">\u672c\u6587\u9690\u85cf\u5185\u5bb9 <a href=\"https:\/\/vip.fxxkpython.com\/wp-login.php?redirect_to=https%3A%2F%2Fvip.fxxkpython.com%2F%3Fp%3D6664\">\u767b\u9646<\/a> \u540e\u624d\u53ef\u4ee5\u6d4f\u89c8<\/div>\u73b0\u5728\u4f60\u7684\u914d\u7f6e\u5df2\u7ecf\u521b\u5efa\u597d\uff0c\u4ee5\u540e\u9700\u8981\u522b\u7684\u914d\u7f6e\uff0c\u4f60\u53ea\u9700\u8981\u5728\u8fd9\u91cc\u9762\u76f8\u5e94\u5bf9\u8c61\u6dfb\u52a0\u5373\u53ef\uff0c\u53ef\u662f\u5982\u4f55\u786e\u5207\u7684\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u4f7f\u7528\u4e0d\u540c\u7684\u914d\u7f6e\u5462\uff1f<\/p>\n\n\n\n<p>\u6211\u4eec\u4e4b\u524d app \u5b9e\u4f8b\u662f\u76f4\u63a5\u5728 init.py \u4e2d\u76f4\u63a5\u5b9a\u4e49\u7684\uff0c\u6211\u4eec\u73b0\u5728\u4f7f\u7528\u4e86\u8fd9\u79cd\u914d\u7f6e\u65b9\u5f0f\u5c31\u4e0d\u597d\u8fd9\u4e48\u5f04\u4e86\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Flask \u63d0\u4f9b\u7684\u5de5\u5382\u65b9\u6cd5\u6765\u521b\u5efa\u5b9e\u4f8b\uff0c\u5728 init.py \u4e2d\u5b9a\u4e49 create_app \u65b9\u6cd5\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># \u5de5\u5382\u65b9\u6cd5\u521b\u5efa\u5b9e\u4f8b\ndef create_app(config_name):\n    # \u52a0\u8f7d\u914d\u7f6e\n    app = Flask('mysystem')\n    app.config.from_object(config[config_name])\n    config[config_name].init_app(app)\n    # \u6ce8\u518c\u84dd\u672c\n    register_blueprints(app)\n    # \u6ce8\u518c\u63d2\u4ef6\n    register_extentions(app)\n    return app\n<\/pre><\/div>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a app \u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u5f53 Flask \u8fd0\u884c\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u8bc6\u522b\u8fd9\u4e2a\u5de5\u5382\u65b9\u6cd5\u8fd0\u884c\uff0c\u8fd9\u91cc\u9762\u7684\u53c2\u6570\u6211\u4eec\u5b9a\u4e49\u4e86 config_name\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u5728\u4e0d\u540c\u573a\u666f\u4e0b\uff0c\u4f20\u5165\u6211\u4eec\u5b9a\u4e49\u597d\u7684\u76f8\u5173\u53c2\u6570\u5c31\u80fd\u5f97\u5230\u76f8\u5173\u7684\u914d\u7f6e\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">config = {\n    &quot;development&quot;: DevConfig,\n    &quot;testing&quot;: TestConfig,\n    &quot;production&quot;: ProConfig,\n    &quot;default&quot;: DevConfig,\n}\n<\/pre><\/div>\n\n\n\n<p>\u6bd4\u5982\u6211\u4eec\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\uff0c\u5c31\u53ef\u4ee5\u8fd9\u6837\u6765\u83b7\u5f97\u6d4b\u8bd5\u73af\u5883\u4e0b\u7684 app \u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">create_app(&quot;testing&quot;)<\/pre><\/div>\n\n\n\n<p>\u8fdb\u800c\u6240\u6709\u7684\u64cd\u4f5c\u53ea\u4f5c\u7528\u4e8e\u6d4b\u8bd5\u73af\u5883\u4e2d\u3002<\/p>\n\n\n\n<p>OK\uff0c\u90a3\u4e48\u7ecf\u8fc7\u8fd9\u6837\u4e00\u756a\u64cd\u4f5c\u4e4b\u540e\uff0c\u4f60\u7684\u9879\u76ee\u7ed3\u6784\u4f1a\u66f4\u52a0\u6e05\u6670\uff0c\u4e5f\u4fbf\u4e8e\u4e4b\u540e\u7684\u6269\u5c55\u3002\u6211\u4eec\u4e0b\u56de\u89c1\uff0cpeace\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u7ed9\u4f60\u5206\u4eab\u8fc7\u5173\u4e8e\u300c\u84dd\u672c\u300d\u7684\u77e5\u8bc6\u70b9\uff1a\u4e00\u4e2a\u9879\u76ee\uff0c\u53ef\u4ee5\u62c6\u5206\u6210\u591a\u4e2a\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u6a21\u5757\u5316\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u84dd\u672c\u53ef\u4ee5\u7edf\u4e00\u7ba1\u7406\u8d44\u6e90\u548c\u64cd\u4f5c\u3002 \u4f60\u7684\u4e2a\u4eba\u7ba1\u7406\u7cfb\u7edf\uff0c\u80af\u5b9a\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684 py \u6a21\u5757\u5c31\u80fd\u641e\u5b9a\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u6700\u597d\u8981\u628a\u84dd\u672c\u4f7f\u7528\u8d77\u6765\uff0c\u8ba9\u9879\u76ee\u66f4\u52a0\u6e05\u6670\u4e00\u4e9b\uff0c\u63a5\u4e0b\u6765\u6211\u4f1a\u7ee7\u7eed&hellip;<\/p>\n","protected":false},"author":1,"featured_media":6093,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[99],"tags":[622,623,549,624,621],"_links":{"self":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6664"}],"collection":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6664"}],"version-history":[{"count":2,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6664\/revisions"}],"predecessor-version":[{"id":6772,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6664\/revisions\/6772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/media\/6093"}],"wp:attachment":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}