{"id":1653,"date":"2018-03-27T21:11:21","date_gmt":"2018-03-27T12:11:21","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=1653"},"modified":"2018-11-04T22:38:05","modified_gmt":"2018-11-04T13:38:05","slug":"flask%e3%81%a7%e4%bd%9c%e3%82%8b%e7%b0%a1%e6%98%93%e5%88%86%e6%9e%90%e3%83%84%e3%83%bc%e3%83%ab-6-%e4%b8%80%e8%a6%a7%e7%94%bb%e9%9d%a2%e3%81%ae%e4%bd%9c%e6%88%90","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/1653","title":{"rendered":"Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb6 \u4e00\u89a7\u753b\u9762\u306e\u4f5c\u6210"},"content":{"rendered":"<p>\u524d\u56de\u307e\u3067\u3067\u5206\u6790\u51e6\u7406\u3068\u7d50\u679c\u306e\u53c2\u7167\u51e6\u7406\u306e\u5b9f\u88c5\u304c\u7d42\u308f\u308a\u307e\u3057\u305f\u3002\u6b21\u306b\u7d50\u679c\u4e00\u89a7\u304c\u53c2\u7167\u3067\u304d\u308b\u30c8\u30c3\u30d7\u30da\u30fc\u30b8\u3092\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u4e00\u89a7\u53c2\u7167\u51e6\u7406\u306e\u5b9f\u88c5<\/h2>\n<h3>models.py\u306e\u4fee\u6b63<\/h3>\n<p>\u4e00\u89a7\u53c2\u7167\u306e\u305f\u3081\u306b\u3001\u7d50\u679c\u5168\u4ef6\u53d6\u5f97\u51e6\u7406\u304c\u5fc5\u8981\u3068\u306a\u308a\u307e\u3059\u3002models.py\u306b\u4ee5\u4e0b\u306e\u95a2\u6570\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef select_all(con):\r\n    &quot;&quot;&quot; SELECT\u3059\u308b &quot;&quot;&quot;\r\n    cur = con.execute('select id, title, data, img, created from results order by id desc')\r\n    return cur.fetchall()\r\n\r\n<\/pre>\n<p>result\u30c6\u30fc\u30d6\u30eb\u306e\u5185\u5bb9\u3092\u5168\u4ef6\u53d6\u5f97\u3057\u3066\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>run.py\u306e\u4fee\u6b63<\/h3>\n<p>\u30e2\u30c7\u30eb\u306e\u547c\u3073\u51fa\u3057\u51e6\u7406\u3092run.py\u306b\u5b9f\u88c5\u3057\u307e\u3059\u3002view\u95a2\u6570\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@app.route('\/')\r\ndef index():\r\n    &quot;&quot;&quot; \u4e00\u89a7\u753b\u9762 &quot;&quot;&quot;\r\n    con = get_db()\r\n    results = models.select_all(con)\r\n    return render_template('index.html', results=results)\r\n\r\n<\/pre>\n<p>DB\u30a2\u30af\u30bb\u30b9\u3057\u3066\u53d6\u5f97\u3057\u305f\u7d50\u679c\u3092\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u6e21\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u4fee\u6b63<\/h3>\n<p>\u4e00\u89a7\u753b\u9762\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u4fee\u6b63\u3057\u307e\u3059\u3002run.py\u304b\u3089\u6e21\u3055\u308c\u305f\u5024\u3092\u8868\u793a\u3059\u308b\u305f\u3081\u306b\u3001index.html\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n{% extends &quot;base.html&quot; %}\r\n{% block body %}\r\n&lt;h1&gt;\u5206\u6790\u4e00\u89a7&lt;\/h1&gt;\r\n&lt;a href=&quot;\/create&quot;&gt;\u65b0\u898f\u5206\u6790&lt;\/a&gt;\r\n&lt;table class=&quot;table table-striped table-hover&quot;&gt;\r\n    &lt;tr&gt;\r\n        &lt;th&gt;id&lt;\/th&gt;\r\n        &lt;th&gt;title&lt;\/th&gt;\r\n        &lt;th&gt;date&lt;\/th&gt;\r\n        &lt;th&gt;\u64cd\u4f5c&lt;\/th&gt;\r\n    &lt;\/tr&gt;\r\n    {% for result in results %}\r\n    &lt;tr&gt;\r\n        &lt;td&gt;{{ result.id }}&lt;\/td&gt;\r\n        &lt;td&gt;{{ result.title|safe}}&lt;\/td&gt;\r\n        &lt;td&gt;{{ result.created }}&lt;\/td&gt;\r\n        &lt;td&gt;\r\n            &lt;a href=&quot;\/view\/{{ result.id }}&quot;&gt;&lt;button class=&quot;btn btn-primary&quot;&gt;\u53c2\u7167&lt;\/button&gt;&lt;\/a&gt;\r\n            &lt;form action=&quot;\/delete\/{{ result.id }}&quot; style=&quot;display: inline&quot; method=&quot;post&quot;&gt;\r\n                &lt;input class=&quot;btn btn-danger&quot; type=&quot;submit&quot; value=&quot;\u524a\u9664&quot; onclick='return confirm(&quot;\u524a\u9664\u3057\u307e\u3059\u304c\u3088\u308d\u3057\u3044\u3067\u3059\u304b\uff1f&quot;)';&gt;\r\n            &lt;\/form&gt;\r\n\r\n        &lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n    {% endfor %}\r\n\r\n&lt;\/table&gt;\r\n\r\n{% endblock %}\r\n<\/pre>\n<p>\u8907\u6570\u7d50\u679c\u3092\u8868\u793a\u3059\u308b\u305f\u3081\u3001for\u6587\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u307e\u305f\u3001\u7d50\u679c\u306e\u524a\u9664\u51e6\u7406\u306e\u305f\u3081\u3001\u524a\u9664\u30d5\u30a9\u30fc\u30e0\u306bpk\u3092hidden\u3067\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u524a\u9664\u51e6\u7406\u306e\u5b9f\u88c5<\/h2>\n<p>\u6b21\u306b\u524a\u9664\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u307e\u3057\u3087\u3046\u3002\u753b\u9762\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<h3>models.py\u306e\u4fee\u6b63<\/h3>\n<p>DB\u306e\u30ec\u30b3\u30fc\u30c9\u524a\u9664\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002models.py\u306b\u4ee5\u4e0b\u3092\u8ffd\u8a18\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef delete(con, pk):\r\n    &quot;&quot;&quot; \u6307\u5b9a\u3057\u305f\u30ad\u30fc\u306e\u30c7\u30fc\u30bf\u3092DELETE\u3059\u308b &quot;&quot;&quot;\r\n    cur = con.cursor()\r\n    cur.execute('delete from results where id=?', (pk,))\r\n    con.commit()\r\n\r\n<\/pre>\n<p>\u6307\u5b9a\u3057\u305fid\u306e\u30ec\u30b3\u30fc\u30c9\u306b\u5bfe\u3057\u3066delete\u6587\u3092\u767a\u884c\u3057\u307e\u3059\u3002<\/p>\n<h3>run.py\u306e\u4fee\u6b63<\/h3>\n<p>delete\u51e6\u7406\u3092\u547c\u3073\u51fa\u3059\u51e6\u7406\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002run.py\u306edelete\u95a2\u6570\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n@app.route('\/delete\/&lt;pk&gt;', methods=['POST'])\r\ndef delete(pk):\r\n    &quot;&quot;&quot; \u7d50\u679c\u524a\u9664\u51e6\u7406 &quot;&quot;&quot;\r\n    con = get_db()\r\n    models.delete(con, pk)\r\n    return redirect(url_for('index'))\r\n\r\n<\/pre>\n<p>\u524a\u9664\u51e6\u7406\u306e\u5b8c\u4e86\u5f8c\u3001Top\u753b\u9762\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3057\u307e\u3059\u3002<\/p>\n<h2>\u52d5\u4f5c\u78ba\u8a8d<\/h2>\n<p>\u304a\u75b2\u308c\u69d8\u3067\u3059\u3002\u3053\u308c\u3067\u4e00\u901a\u308a\u306e\u6a5f\u80fd\u304c\u5b9f\u88c5\u3055\u308c\u307e\u3057\u305f\u3002\u4e00\u89a7\u753b\u9762\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3001\u767b\u9332\u3057\u305f\u30c7\u30fc\u30bf\u304c\u8868\u793a\u3055\u308c\u308b\u3053\u3068\u3001\u524a\u9664\u304c\u3067\u304d\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>http:\/\/localhost:5000<\/p>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_index_conf.png\"><img loading=\"lazy\" src=\"http:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_index_conf.png\" alt=\"\" width=\"1253\" height=\"702\" class=\"alignnone size-full wp-image-1705\" srcset=\"https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_index_conf.png 1253w, https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_index_conf-300x168.png 300w, https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_index_conf-768x430.png 768w, https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_index_conf-1024x574.png 1024w\" sizes=\"(max-width: 1253px) 100vw, 1253px\" \/><\/a>\n<p>\u4eca\u56de\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u306b\u6563\u5e03\u56f3\u884c\u5217\u3092\u4f7f\u3044\u307e\u3057\u305f\u304c\u3001Python\u3092\u4f7f\u3048\u3070\u4e3b\u6210\u5206\u5206\u6790\u3084K-means\u306b\u3088\u308b\u30af\u30e9\u30b9\u30bf\u30fc\u5206\u6790\u3068\u3044\u3063\u305f\u8868\u8a08\u7b97\u30bd\u30d5\u30c8\u3067\u96e3\u3057\u3044\u5206\u6790\u51e6\u7406\u3092\u7c21\u5358\u306bWeb\u4e0a\u306b\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u307f\u306a\u3055\u307e\u3082\u65e5\u3005\u4f7f\u7528\u3059\u308b\u51e6\u7406\u306eWeb\u5316\u306b\u6311\u6226\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u56de\u307e\u3067\u3067\u5206\u6790\u51e6\u7406\u3068\u7d50\u679c\u306e\u53c2\u7167\u51e6\u7406\u306e\u5b9f\u88c5\u304c\u7d42\u308f\u308a\u307e\u3057\u305f\u3002\u6b21\u306b\u7d50\u679c\u4e00\u89a7\u304c\u53c2\u7167\u3067\u304d\u308b\u30c8\u30c3\u30d7\u30da\u30fc\u30b8\u3092\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002 \u4e00\u89a7\u53c2\u7167\u51e6\u7406\u306e\u5b9f\u88c5 models.py\u306e\u4fee\u6b63 \u4e00\u89a7\u53c2\u7167\u306e\u305f\u3081\u306b\u3001\u7d50\u679c\u5168\u4ef6\u53d6\u5f97\u51e6\u7406\u304c\u5fc5\u8981\u3068\u306a\u308a\u307e\u3059\u3002model...<\/p>\n","protected":false},"author":1,"featured_media":1719,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[153,3,9],"tags":[171,170],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1653"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":7,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"predecessor-version":[{"id":1972,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1653\/revisions\/1972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/1719"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}