{"id":6438,"date":"2021-05-24T22:37:00","date_gmt":"2021-05-24T14:37:00","guid":{"rendered":"https:\/\/vip.fxxkpython.com\/?p=6438"},"modified":"2021-05-25T22:52:41","modified_gmt":"2021-05-25T14:52:41","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-13-%e7%af%87-%ef%bd%9c-%e7%99%bb%e5%bd%95%e6%b5%81","status":"publish","type":"post","link":"https:\/\/vip.fxxkpython.com\/?p=6438","title":{"rendered":"\u4f7f\u7528 Python \u4ece 0 \u5f00\u53d1\u5c5e\u4e8e\u4f60\u7684\u4e2a\u4eba\u7ba1\u7406\u7cfb\u7edf 13 \u7bc7 \uff5c \u767b\u5f55\u6d41\u7a0b\u4ece\u524d\u7aef\u5230\u540e\u7aef\u7684\u5b9e\u73b0"},"content":{"rendered":"\n<p>\u901a\u8fc7\u524d\u9762\u7684\u5b66\u4e60\uff0c\u4f60\u5df2\u7ecf\u77e5\u9053\u4e86 Flask \u4e2d\u5bf9 Form \u8868\u5355\u7684\u4f7f\u7528\uff0c\u4e5f\u77e5\u9053\u4e86\u6570\u636e\u5e93\u521b\u5efa\u7528\u6237\u8868\uff0c\u4ee5\u53ca\u4f7f\u7528\u4e86\u547d\u4ee4\u8fdb\u884c\u4e86\u7528\u6237\u7684\u6ce8\u518c\u548c\u767b\u5f55\u6821\u9a8c\uff0c\u90a3\u4e48\u5982\u4f55\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u9879\u76ee\u4e2d\u53bb\u5462\uff1f\u4e5f\u5c31\u662f\u8bf4\uff0c\u4ece\u524d\u7aef\u7684\u9875\u9762\u5230\u540e\u7aef\u7684\u6574\u4e2a\u6d41\u7a0b\u7684\u5b9e\u73b0\u662f\u600e\u4e48\u6837\u7684\uff1f<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/008i3skNgy1gqv1ktfi90g31am0k34dt.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u63a5\u4e0b\u6765\u8ddf\u7740\u5c0f\u5e05b\u4e00\u8d77\u5b9e\u73b0\u8d77\u6765\u5427\u3002<\/p>\n\n\n\n<h2>\u524d\u7aef\u767b\u5f55\u6821\u9a8c<\/h2>\n\n\n\n<p>\u5728\u8def\u7531\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u9875\u9762\u7684\u6e32\u67d3\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;}\">@app.route('\/login', methods=['GET'])\ndef login():\n    return render_template('login.html', form=login_form)\n<\/pre><\/div>\n\n\n\n<p>\u73b0\u5728\u5c06\u4f60\u7684\u9879\u76ee\u8fd0\u884c\u8d77\u6765\uff0c\u8bbf\u95ee login \u8def\u7531\uff0c\u5448\u73b0\u7684\u662f\u8fd9\u4e2a\u767b\u5f55\u9875\u9762\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"505\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.11.34-1024x505.png\" alt=\"\" class=\"wp-image-6439\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.11.34-1024x505.png 1024w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.11.34-300x148.png 300w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.11.34-768x378.png 768w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.11.34-1536x757.png 1536w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.11.34-2048x1009.png 2048w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.11.34-769x379.png 769w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u73b0\u5728\u5b83\u662f\u9759\u6001\u7684\uff0c\u4ec0\u4e48\u90fd\u6ca1\u6709\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u5728\u524d\u7aef\u7528\u6237\u70b9\u51fb\u767b\u5f55\u7684\u65f6\u5019\uff0c\u8bf7\u6c42\u524d\u6211\u4eec\u5e0c\u671b\u5b83\u4f1a\u5148\u8fdb\u884c\u4e00\u4e9b\u9a8c\u8bc1\uff0c\u6bd4\u5982\u7528\u6237\u540d\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u5bc6\u7801\u4e0d\u80fd\u5c11\u4e8e 5 \u4e2a\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a Form \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;}\">class LoginForm(FlaskForm):\n    username = StringField('username', validators=[DataRequired()])\n    password = PasswordField('password', validators=[DataRequired(), Length(5, 100)])\n    remember_pwd = BooleanField('remember_pwd')\n    login = SubmitField('login')\n<\/pre><\/div>\n\n\n\n<p>\u5728 HTML \u4e2d\u7ed1\u5b9a\u76f8\u5173\u53c2\u6570\u5c31\u53ef\u4ee5\u4e86\uff0c\u5177\u4f53\u53ef\u4ee5\u770b<a href=\"https:\/\/vip.fxxkpython.com\/?p=6423\">\u524d\u9762\u7684\u4ecb\u7ecd<\/a>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"600\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.35.50-1024x600.png\" alt=\"\" class=\"wp-image-6440\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.35.50-1024x600.png 1024w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.35.50-300x176.png 300w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.35.50-768x450.png 768w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.35.50-1536x900.png 1536w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.35.50-769x450.png 769w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2021\/05\/\u622a\u5c4f2021-05-25-\u4e0b\u53488.35.50.png 1946w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2>\u540e\u53f0\u767b\u5f55\u6821\u9a8c<\/h2>\n\n\n\n<p>\u5728 HTML \u7684 form \u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u8bf7\u6c42\u7684\u8def\u5f84:<\/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;}\">&lt;form class=&quot;user&quot; action=&quot;\/userlogin&quot; method=&quot;post&quot;&gt;<\/pre><\/div>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728 userlogin \u65b9\u6cd5\u4e2d\u505a\u767b\u5f55\u7684\u6821\u9a8c\u64cd\u4f5c\u4e86\u3002<\/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;}\">@app.route('\/userlogin', methods=['POST'])\ndef userlogin():\n    login_form = LoginForm()\n    if login_form.validate_on_submit():\n        # \u83b7\u53d6\u4f20\u8fc7\u6765\u7684\u8868\u5355\u6570\u636e\u8fdb\u884c\u6570\u636e\u6821\u9a8c\n        ....\n        ....\n        return render_template('index.html', title='\u4e2a\u4eba\u7ba1\u7406\u7cfb\u7edf')\n    return render_template('login.html', form=login_form)\n\n<\/pre><\/div>\n\n\n\n<p>login_form.validate_on_submit \u5224\u65ad\u524d\u7aef\u6821\u9a8c\uff0c\u5982\u679c\u901a\u8fc7\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u4f20\u6765\u7684\u6570\u636e\u8fdb\u884c\u6821\u9a8c\u3002<\/p>\n\n\n\n<p>\u83b7\u53d6\u4f20\u8fc7\u6765\u7684\u8d26\u53f7\u5bc6\u7801\uff1a<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%3D6438\">\u767b\u9646<\/a> \u540e\u624d\u53ef\u4ee5\u6d4f\u89c8<\/div>\u521a\u521a\u6211\u4eec\u5728\u7528\u6237\u767b\u5f55\u6210\u529f\u7684\u65f6\u5019\u5c06\u7528\u6237\u7684 id \u6dfb\u52a0\u5230 session\uff0c\u90a3\u4e48\u767b\u51fa\u7684\u65f6\u5019\u53ea\u9700\u8981\u5c06 id \u79fb\u9664\u6389\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u5728 index.html \u4e2d\uff0c\u5c06 a \u6807\u7b7e\u5b9a\u4e49\u4e3a\u8c03\u7528 logout \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;}\">\n&lt;div class=&quot;modal-footer&quot;&gt;\n     &lt;button class=&quot;btn btn-secondary&quot; type=&quot;button&quot; data-dismiss=&quot;modal&quot;&gt;Cancel&lt;\/button&gt;\n     &lt;a class=&quot;btn btn-primary&quot; href=&quot;{{ url_for('logout') }}&quot; &gt;Logout&lt;\/a&gt;\n&lt;\/div&gt;\n\n<\/pre><\/div>\n\n\n\n<p>\u63a5\u7740\u5728\u8def\u7531\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49 logout \u65b9\u6cd5\uff0c\u5c06 session \u4e2d\u7684 id \u79fb\u9664\uff0c\u7136\u540e\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\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;}\">@app.route('\/logout', methods=['GET'])\ndef logout():\n    del session['user_id']\n    return redirect(url_for('login'))\n\n<\/pre><\/div>\n\n\n\n<p>OK\uff0c\u90a3\u4e48\u5230\u73b0\u5728\u6211\u4eec\u7684\u6574\u4e2a\u767b\u5f55\u7684\u6d41\u7a0b\u5c31\u5b9e\u73b0\u4e86\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/008i3skNgy1gqv1ktfi90g31am0k34dt.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u90a3\u4e48\u5bf9\u4e8e\u4e00\u4e9b\u65b9\u6cd5\u7684\u8bf7\u6c42\uff0c\u5728\u8bf7\u6c42\u524d\uff0c\u6709\u4e9b\u65b9\u6cd5\u662f\u53ef\u4ee5\u76f4\u63a5\u88ab\u8bbf\u95ee\uff0c\u6709\u4e9b\u65b9\u6cd5\u662f\u9700\u8981\u767b\u5f55\u4e86\u624d\u8bbf\u95ee\uff0c\u5982\u4f55\u624d\u80fd\u505a\u597d\u8fd9\u4e00\u70b9\u9a8c\u8bc1\u5462\uff1f\u4f60\u53ef\u4ee5\u60f3\u60f3\u6211\u4eec\u4e4b\u524d\u8bb2\u8fc7\u7684\u88c5\u9970\u5668\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4e0b\u7bc7\u7ee7\u7eed\uff0cpeace\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u524d\u9762\u7684\u5b66\u4e60\uff0c\u4f60\u5df2\u7ecf\u77e5\u9053\u4e86 Flask \u4e2d\u5bf9 Form \u8868\u5355\u7684\u4f7f\u7528\uff0c\u4e5f\u77e5\u9053\u4e86\u6570\u636e\u5e93\u521b\u5efa\u7528\u6237\u8868\uff0c\u4ee5\u53ca\u4f7f\u7528\u4e86\u547d\u4ee4\u8fdb\u884c\u4e86\u7528\u6237\u7684\u6ce8\u518c\u548c\u767b\u5f55\u6821\u9a8c\uff0c\u90a3\u4e48\u5982\u4f55\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u9879\u76ee\u4e2d\u53bb\u5462\uff1f\u4e5f\u5c31\u662f\u8bf4\uff0c\u4ece\u524d\u7aef\u7684\u9875\u9762\u5230\u540e\u7aef\u7684\u6574\u4e2a\u6d41\u7a0b\u7684\u5b9e\u73b0\u662f\u600e\u4e48\u6837\u7684\uff1f \u63a5\u4e0b\u6765\u8ddf\u7740\u5c0f\u5e05b\u4e00\u8d77\u5b9e&hellip;<\/p>\n","protected":false},"author":1,"featured_media":6443,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[99],"tags":[595,596,593,597],"_links":{"self":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6438"}],"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=6438"}],"version-history":[{"count":3,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6438\/revisions"}],"predecessor-version":[{"id":6446,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6438\/revisions\/6446"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/media\/6443"}],"wp:attachment":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}