{"id":310,"date":"2022-02-06T12:21:24","date_gmt":"2022-02-06T04:21:24","guid":{"rendered":"http:\/\/madapapa.com\/wordpress\/?p=310"},"modified":"2022-02-06T16:52:07","modified_gmt":"2022-02-06T08:52:07","slug":"flask002","status":"publish","type":"post","link":"http:\/\/madapapa.com\/wordpress\/?p=310","title":{"rendered":"Flask@002"},"content":{"rendered":"<pre><code class=\"language-plain_text\">\u201cBy default Flask looks for templates in a templates subdirectory \nlocated inside the main application directory. For the next version of \nhello.py, you need to create the templates subdirectory and store the \ntemplates defined in the previous examples in it as index.html and \nuser.html, respectively.\u201d\n\nExcerpt From\nFlask Web Development\nMiguel Grinberg\nThis material may be protected by copyright.\n<\/code><\/pre>\n<p>\u6309\u7167\u72fc\u4e66\u7684\u8bf4\u660e\uff0c\u4f7f\u7528templates\u7684\u597d\u5904\u662f\uff0c\u628a\u4e1a\u52a1\u903b\u8f91\u548c\u4ee3\u7801\u7684\u5448\u73b0\u903b\u8f91\u8fdb\u884c\u4e86\u9694\u79bb\uff0c\u8fd9\u6837\u7684\u597d\u5904\u662f\u975e\u5e38\u6e05\u6670\uff0c\u4e0d\u4f1a\u8ba9\u7a0b\u5e8f\u5458\u7684\u601d\u8def\u6df7\u4e71\uff0c\u5f53\u4f7f\u7528git checkout 3a\u540e\uff0c\u68c0\u67e5\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-plain_text\">(venv) MDMacPro\/Users\/madapapa\/Study\/madapapa.com\/flaskr\/flaskproject\/\nflaskygit:(e020af8) [12:20]ls\nLICENSE     __pycache__ templates\nREADME.md   hello.py\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/madapapa.com\/wordpress\/wp-content\/uploads\/2022\/02\/16441217047426.jpg\" alt=\"\" \/><\/p>\n<h2><a id=\"debug-mode\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>debug mode<\/h2>\n<pre><code class=\"language-plain_text\">\u201c(venv) $ export FLASK_APP=hello.py\n(venv) $ export FLASK_DEBUG=1\n(venv) $ flask run\u201d\n\nExcerpt From\nFlask Web Development\nMiguel Grinberg\nThis material may be protected by copyright.\n<\/code><\/pre>\n<p>\u901a\u8fc7export FLASK_DEBUG=1\u5f00\u542fdebug\u6a21\u5f0f\uff0c\u6709\u4e24\u4e2a\u597d\u5904<\/p>\n<ul>\n<li>reloader\u6a21\u5f0f\u53ef\u4ee5\u5728\u4fee\u6539\u6e90\u4ee3\u7801\u7684\u540c\u65f6\uff0c\u81ea\u52a8\u91cd\u542f\u670d\u52a1\u5668\uff0c\u4ee3\u7801\u7684\u4fee\u6539\u7ed3\u679c\u5728\u8fd0\u884c\u73af\u5883\u4e0b\u81ea\u52a8\u751f\u6548\uff0c\u5bf9\u8c03\u8bd5\u975e\u5e38\u53cb\u597d<\/li>\n<li>debugger\u6a21\u5f0f\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684\u6d4f\u89c8\u5668\u663e\u793a\u9519\u8bef\u4fe1\u606f\uff0c\u4e0d\u8fc7\u5728\u751f\u4ea7\u73af\u5883\u4e0b\u7edd\u5bf9\u4e0d\u80fd\u6253\u5f00\u8fd9\u4e2a\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<h2><a id=\"template%E5%92%8Cplaceholder\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>template\u548cplaceholder<\/h2>\n<p>template\u662f\u5305\u542b\u4e86\u54cd\u5e94\u5185\u5bb9\u7684\u6587\u4ef6\uff0c\u4f7f\u7528placeholder\u4f5c\u4e3a\u4e00\u4e2a\u53d8\u91cf\u670d\u52a1\u52a8\u6001\u90e8\u5206\uff0c\u8fd9\u4e2a\u52a8\u6001\u90e8\u5206\u5305\u542b\u5728\u8bf7\u6c42\u7684\u6587\u672c\u91cc\u3002\u4f7f\u7528\u5b9e\u9645\u503c\u6765\u4ee3\u66ff\u53d8\u91cf\uff0c\u5e76\u4e14\u8fd4\u56de\u6700\u7ec8\u54cd\u5e94\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\uff0c\u5c31\u662f\u6e32\u67d3\uff08rendering\uff09\uff0cflask\u4f7f\u7528\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6a21\u7248\u5f15\u64ce\uff1aJinja2\u3002<\/p>\n<p>Jinja\u662f\u65e5\u8bed\u795e\u5e99\u7684\u610f\u601d\u3002<\/p>\n<h3><a id=\"render-template%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>render_template\u51fd\u6570<\/h3>\n<p>render_template\u51fd\u6570\u4f7f\u7528Jinja2\u5f15\u64ce\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u540d\u201cindex.html\u201d,\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2akey-values\u7684\u952e\u503c\u5bf9\uff0c\u5176\u4e2dna\u662fuser.html\u4e2d\u7684\u53c2\u6570\u540d\u79f0\uff0cname1\u662f\u9700\u8981\u5e26\u5165\u7684\u771f\u5b9e\u53c2\u6570\u503c\u3002<\/p>\n<pre><code class=\"language-plain_text\">from flask import Flask, render_template\napp = Flask(__name__)\n\n@app.route('\/')\ndef index():\n    return render_template('index.html')\n\n@app.route('\/user\/&lt;name1&gt;')\ndef user(name1):\n    return render_template('user.html', na=name1)\n<\/code><\/pre>\n<p>user.html\u6587\u4ef6\u7684\u5185\u5bb9<\/p>\n<pre><code class=\"language-plain_text\">&lt;h1&gt;Hello, {{ na }}!&lt;\/h1&gt;\n<\/code><\/pre>\n<p>{{na}}\u8fd9\u79cd\u7ed3\u6784\uff0c\u53ef\u4ee5\u5e26\u5165\u5404\u79cd\u53c2\u6570\u683c\u5f0f,\u6bd4\u5982\u5b57\u5178\u3001\u5217\u8868\u3001\u65b9\u6cd5\u7b49\u7b49\u3002<\/p>\n<pre><code class=\"language-plain_text\">\u201c&lt;p&gt;A value from a dictionary: {{ mydict['key'] }}.&lt;\/p&gt;\n\u201c&lt;p&gt;A value from a list: {{ mylist[3] }}.&lt;\/p&gt;\u201d\n\u201c&lt;p&gt;A value from a list with variable index\uff1a{{ mylist[myvar] }}.&lt;\/p&gt;\u201d\n\u201c&lt;p&gt;A value from an object's method: {{ myobj.somemethod() }}.&lt;\/p&gt;\u201d\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u201cBy default Flask looks for templates in a templates subdirectory located inside the main application directory. For the next version of hello.py, you need to create the templates subdirectory and store the templates defined in the previous examples in it as index.html and user.html, respectively.\u201d Excerpt From Flask Web Development Miguel Grinberg This material may &hellip; <a href=\"http:\/\/madapapa.com\/wordpress\/?p=310\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Flask@002<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[5,11],"tags":[],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry","category-linux","category-tech"],"_links":{"self":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=310"}],"version-history":[{"count":5,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":316,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions\/316"}],"wp:attachment":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}