{"id":177575,"date":"2023-07-30T07:59:57","date_gmt":"2023-07-29T23:59:57","guid":{"rendered":"http:\/\/www.idc.net\/help\/177575\/"},"modified":"2023-07-30T07:59:57","modified_gmt":"2023-07-29T23:59:57","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9aflask%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae%e8%b7%af%e7%94%b1","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/177575\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1aflask\u600e\u4e48\u8bbe\u7f6e\u8def\u7531"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/004\/5d3bf1c066ba3176.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u901a\u5e38\u6709\u4ee5\u4e0b\u51e0\u79cd\u5b9a\u4e49\u8def\u7531\u51fd\u6570\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>1.\u4f7f\u7528flask.Flask.route() \u4fee\u9970\u5668\u3002<\/p>\n<p>2.\u4f7f\u7528flask.Flask.add_url_rule()\u51fd\u6570\u3002<\/p>\n<p>3.\u76f4\u63a5\u8bbf\u95ee\u57fa\u4e8ewerkzeug\u8def\u7531\u7cfb\u7edf\u7684flask.Flask.url_map.<\/p>\n<p><strong>\u4e00 \u3001\u8ba9\u6211\u4eec\u4ece\u6700\u5e38\u7528\u7684@app.route()\u4fee\u9970\u5668\u5f00\u59cb\u3002<\/strong><\/p>\n<pre>def&nbsp;route(self,&nbsp;rule,&nbsp;**options):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;decorator(f):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;endpoint&nbsp;=&nbsp;options.pop('endpoint',&nbsp;None)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.add_url_rule(rule,&nbsp;endpoint,&nbsp;f,&nbsp;**options)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;f\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;decorator<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u4fee\u9970\u5668\u662f\u5bf9add_url_rule\u51fd\u6570\u7684\u5305\u88c5\uff0c\u5f53\u6211\u4eec\u5199\u5982\u4e0b\u4ee3\u7801\u65f6\uff1a<\/p>\n<pre>@app.route('\/index.html')\ndef&nbsp;index():\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;\"Hello&nbsp;World!\"<\/pre>\n<p>\u5b9e\u9645\u4e0a\u4e0a\u9762\u7684\u4ee3\u7801\u8f6c\u6362\u6210\uff1a<\/p>\n<pre>def&nbsp;index():\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;\"Hello&nbsp;World!\"\nindex&nbsp;=&nbsp;app.route('\/index.html')(index)<\/pre>\n<p>\u4e5f\u5c31\u662f\uff0crule = '\/index.html', options = { }, \u6267\u884cdecorator(index) \u65f6\u4f1a\u6267\u884cself.add_url_rule(rule, endpoint, f, **options)<\/p>\n<p><strong>\u4e8c\u3001<\/strong>\u4e0b\u9762\u56de\u8fc7\u5934\uff0c\u6765\u770b\u770b\u5f53Flask\u8fd0\u884c\u65f6\uff0c\u4e00\u4e2aRequest\u6765\u4e86\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff0c\u4ecd\u7136\u4eceFlask.wsgi_app\u5f00\u59cb\u9605\u8bfb\u3002<br \/>\u5df2\u7ecf\u77e5\u9053\uff0c\u5f53\u4e00\u4e2aRequest\u5230\u6765\u65f6\uff0c\u4f1a\u9996\u5148push RequestContext\u548cAppContext\uff0c\u5728RequestContext\u4e2d\u7684init\u51fd\u6570\u4e2d\u6709:<\/p>\n<pre>...self.url_adapter&nbsp;=&nbsp;app.create_url_adapter(self.request)\n...self.match_request()<\/pre>\n<pre>def&nbsp;create_url_adapter(self,&nbsp;request):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;request&nbsp;is&nbsp;not&nbsp;None:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self.url_map.bind_to_environ(request.environ,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server_name=self.config['SERVER_NAME'])\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<\/pre>\n<p>\u9996\u5148\u5c06Flask.url_map\u4e0e\u5f53\u524d\u5230\u6765\u7684Request\u4e2denviron\u8fdb\u884c\u7ed1\u5b9a\uff0c\u83b7\u5f97\u4e00\u4e2aurl_adapter\u3002<\/p>\n<pre>def&nbsp;match_request(self):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url_rule,&nbsp;self.request.view_args&nbsp;=&nbsp;\\\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.url_adapter.match(return_rule=True)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.request.url_rule&nbsp;=&nbsp;url_rule\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;except&nbsp;HTTPException&nbsp;as&nbsp;e:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.request.routing_exception&nbsp;=&nbsp;e<\/pre>\n<p>\u83b7\u5f97url_adaptor\u4e4b\u540e\uff0c\u8c03\u7528match_request\uff0curl_adapter.match()\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4view_args\u5c31\u662furl_rule\u4e2d\u7684\u53c2\u6570\uff0c\u6bd4\u5982Rule(\/&lt;int:year&gt;\/, endpoint='blog\/archive')\u8fd9\u4e2aRule,\u800c\u8bf7\u6c42\u662f\/2016\/\uff0c\u90a3\u4e48view_args={year: 2016}. url_rule\u548cview_args\u88ab\u50a8\u5b58\u5728Request\u4e2d\u3002\u5728Request\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5Request.endpoint\u5c06\u8fd4\u56deurl_rule.endpoint.<\/p>\n<p>\u5728url_rule\u548cview_args\u88ab\u88c5\u8f7d\u5230Request\u4e2d\u540e\uff0c\u6211\u4eec\u7ee7\u7eed\u5bf9wsgi_app\u4e2d\u7684response = self.full_dispatch_request()\u8fd9\u4e2a\u8fc7\u7a0b\u4e0e\u8def\u7531\u76f8\u5173\u7684\u5185\u5bb9\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<pre>&nbsp;def&nbsp;full_dispatch_request(self):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.try_trigger_before_first_request_functions()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request_started.send(self)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rv&nbsp;=&nbsp;self.preprocess_request()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;rv&nbsp;is&nbsp;None:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rv&nbsp;=&nbsp;self.dispatch_request()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<\/pre>\n<p>dispatch_request()\u5904\u7406\u5b8c\u6bd5\uff0c\u5c06\u8fd4\u56de\u503c\u50a8\u5b58\u5728rv\u53d8\u91cf\u4e2d\u3002\u901a\u5e38\uff0c\u89c6\u56fe\u51fd\u6570\u4f1areturn render_template(...). \u8fd4\u56de\u503c\u63a5\u4e0b\u6765\u7ecf\u8fc7\u4e00\u7cfb\u5217\u5904\u7406\uff0c\u53d1\u9001\u5230\u5ba2\u6237\u7aef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u5e38\u6709\u4ee5\u4e0b\u51e0\u79cd\u5b9a\u4e49\u8def\u7531\u51fd\u6570\u7684\u65b9\u6cd5\uff1a 1.\u4f7f\u7528flask.Flask.route() \u4fee\u9970\u5668\u3002 2.\u4f7f\u7528flas [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":177576,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-177575","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/177575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=177575"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/177575\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/177576"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=177575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=177575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=177575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}