{"id":1376,"date":"2019-05-08T06:37:18","date_gmt":"2019-05-07T21:37:18","guid":{"rendered":"http:\/\/idealive.jp\/blog\/?p=1376"},"modified":"2019-05-08T06:37:18","modified_gmt":"2019-05-07T21:37:18","slug":"%e6%a4%9c%e7%b4%a2%e6%a9%9f%e8%83%bd%e3%82%92%e8%bf%bd%e5%8a%a0%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/idealive.jp\/blog\/2019\/05\/08\/%e6%a4%9c%e7%b4%a2%e6%a9%9f%e8%83%bd%e3%82%92%e8%bf%bd%e5%8a%a0%e3%81%99%e3%82%8b\/","title":{"rendered":"\u691c\u7d22\u6a5f\u80fd\u3092\u8ffd\u52a0\u3059\u308b"},"content":{"rendered":"<p>\u524d\u56de\u3067\u306fSQLite3\u306b\u767b\u9332\u3055\u308c\u305f\u5546\u54c1\u60c5\u5831\u3092\u8aad\u307f\u8fbc\u3093\u3067Web\u30da\u30fc\u30b8\u306b\u8868\u793a\u3057\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306f\u305d\u306e\u5546\u54c1\u60c5\u5831\u3092\u691c\u7d22\u3059\u308b\u6a5f\u80fd\u3092\u4f5c\u6210\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">instance\r\n\u2514\u2500\u2500 create_db.py\r\n\u2514\u2500\u2500 itemdb.sqlite3\r\nweb\r\n\u2514\u2500\u2500 itemdb.py\r\n\u2514\u2500\u2500 __init__.py\r\n\u2514\u2500\u2500 templates\r\n|  \u2514\u2500\u2500 base.html\r\n|  \u2514\u2500\u2500 index.html    (1)\u30fb\u30fb\u30fb\u3000\u66f4\u65b0\r\n\u2514\u2500\u2500 items.py         (2)\u30fb\u30fb\u30fb\u3000\u66f4\u65b0 \r\n\u2514\u2500\u2500 static\r\n   \u2514\u2500\u2500 style.css<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>(1)index.html<\/p>\n<p>\u5546\u54c1\u540d\u3092\u691c\u7d22\u3059\u308b\u305f\u3081\u306e\u5165\u529b\u30d5\u30a9\u30fc\u30e0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\" title=\"index.html\">{% extends 'base.html' %}\r\n\r\n{% block title %}Search{% endblock %}\r\n\r\n{% block content %}\r\n    &lt;div class=\"message\"&gt;{{message}}&lt;\/div&gt;\r\n    &lt;form method=\"post\"&gt;\r\n        &lt;label for=\"item_name\"&gt;\u5546\u54c1\u540d&lt;\/label&gt;\r\n        &lt;input name=\"item_name\"&gt;\r\n        &lt;input type=\"submit\" value=\"\u691c\u7d22\"&gt;\r\n    &lt;\/form&gt;\r\n    &lt;table&gt;\r\n        &lt;tr class='keyname'&gt;\r\n            &lt;th&gt;\u5546\u54c1\u540d&lt;\/th&gt;\r\n            &lt;th&gt;\u4fa1\u683c&lt;\/th&gt;\r\n        &lt;\/tr&gt;\r\n        {% for item in items %}\r\n            &lt;tr&gt;\r\n            &lt;td&gt;{{item['item_name']}}&lt;\/td&gt;\r\n            &lt;td&gt;{{item['price']}}&lt;\/td&gt;\r\n        {% endfor %}\r\n    &lt;\/table&gt;\r\n{%  endblock %}\r\n<\/pre>\n<p>\u30fb\uff16\uff5e\uff11\uff11\u884c\u76ee\uff1a\u691c\u7d22\u6a5f\u80fd\u3092\u8ffd\u52a0\uff08\u8981\u6c42\u5f62\u5f0f\u306f\u300cPOST\u300d\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u540d\u306f\u300citem_name\u300d\uff09<\/p>\n<hr \/>\n<p>(2)items.py<\/p>\n<p>SQLite3\u306b\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u5546\u54c1\u60c5\u5831\u3092\u691c\u7d22\u3057\u3066\u3001\u691c\u7d22\u7d50\u679c\u3092index.html\u3078\u623b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\" title=\"items.py\">from flask import (\r\n    Blueprint, render_template, request\r\n)\r\n\r\nfrom web.itemdb import get_db\r\n\r\nbp = Blueprint('items', __name__)\r\n@bp.route('\/', methods=('GET', 'POST'))\r\ndef index():\r\n    \r\n    db=get_db()\r\n    alldata = db.execute('SELECT * FROM items').fetchall()\r\n\r\n    if request.method == 'POST':\r\n        item_name = request.form['item_name']\r\n        message = '\u5546\u54c1\u540d\u300c'+item_name+'\u300d\u3092\u542b\u3080\u5546\u54c1\u60c5\u5831\u306f'\r\n\r\n        searchdata = db.execute(\r\n            \"SELECT * FROM items WHERE item_name like '%\"+item_name+\"%'\").fetchall()\r\n        if len(searchdata)&gt;0:\r\n            message += '\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002'\r\n        else:\r\n            searchdata = alldata\r\n            message += '\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002'\r\n        return render_template('index.html', message=message, items=searchdata)\r\n    else:\r\n        message='\u304a\u63a2\u3057\u306e\u5546\u54c1\u3092\u691c\u7d22\u3057\u3066\u304f\u3060\u3055\u3044\u3002'\r\n        return render_template('index.html', message=message, items=alldata)<\/pre>\n<p>\u30fb\uff12\u884c\u76ee\uff1arequest\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u3092\u8ffd\u52a0<\/p>\n<p>\u30fb\uff18\u884c\u76ee\uff1amethods\u3092\u8ffd\u52a0\uff08\u6700\u521d\u306b\u958b\u3044\u305f\u3068\u304d[GET]\u3068\u3001\u5024\u3092\u9001\u4fe1\u3057\u305f\u3068\u304d[POST]\uff09<\/p>\n<p>\u30fb\uff11\uff14\uff5e\uff12\uff15\u884c\u76ee\uff1aPOST\u306e\u5834\u5408\u3067\u306e\u51e6\u7406<\/p>\n<p>\u30fb\uff12\uff16\uff5e\uff12\uff18\u884c\u76ee\uff1aPOST\u4ee5\u5916\uff08GET\uff09\u306e\u5834\u5408\u3067\u306e\u51e6\u7406<\/p>\n<hr \/>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u30fb\u691c\u7d22\u524d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1386\" src=\"https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/1-3-292x300.png\" alt=\"\" width=\"292\" height=\"300\" srcset=\"https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/1-3-292x300.png 292w, https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/1-3.png 453w\" sizes=\"(max-width: 292px) 100vw, 292px\" \/><\/p>\n<p>\u30fb\u691c\u7d22\u5f8c\uff08\u60c5\u5831\u3042\u308a\uff1a\u691c\u7d22\u306b\u30d2\u30c3\u30c8\u3057\u305f\u5546\u54c1\u60c5\u5831\u306e\u307f\u8868\u793a\uff09<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1381\" src=\"https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/2-292x300.png\" alt=\"\" width=\"292\" height=\"300\" srcset=\"https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/2-292x300.png 292w, https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/2.png 453w\" sizes=\"(max-width: 292px) 100vw, 292px\" \/><\/p>\n<p>\u30fb\u691c\u7d22\u5f8c\uff08\u60c5\u5831\u306a\u3057\uff1a\u5546\u54c1\u60c5\u5831\u3092\u3059\u3079\u3066\u8868\u793a\uff09<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1382\" src=\"https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/3-292x300.png\" alt=\"\" width=\"292\" height=\"300\" srcset=\"https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/3-292x300.png 292w, https:\/\/idealive.jp\/blog\/wp-content\/uploads\/2019\/05\/3.png 453w\" sizes=\"(max-width: 292px) 100vw, 292px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u56de\u3067\u306fSQLite3\u306b\u767b\u9332\u3055\u308c\u305f\u5546\u54c1\u60c5\u5831\u3092\u8aad\u307f\u8fbc\u3093\u3067Web\u30da\u30fc\u30b8\u306b\u8868\u793a\u3057\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306f\u305d\u306e\u5546\u54c1\u60c5\u5831\u3092\u691c\u7d22\u3059\u308b\u6a5f\u80fd\u3092\u4f5c\u6210\u3057\u3066\u3044\u304d\u307e\u3059\u3002 instance \u2514\u2500\u2500 create_db.py \u2514\u2500\u2500 itemdb.sqli&#8230;<a class=\"read-more-link button\" href=\"https:\/\/idealive.jp\/blog\/2019\/05\/08\/%e6%a4%9c%e7%b4%a2%e6%a9%9f%e8%83%bd%e3%82%92%e8%bf%bd%e5%8a%a0%e3%81%99%e3%82%8b\/\">\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":9,"featured_media":1379,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13],"tags":[],"class_list":["post-1376","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-database"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/posts\/1376"}],"collection":[{"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/comments?post=1376"}],"version-history":[{"count":7,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/posts\/1376\/revisions"}],"predecessor-version":[{"id":1390,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/posts\/1376\/revisions\/1390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/media\/1379"}],"wp:attachment":[{"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/media?parent=1376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/categories?post=1376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/tags?post=1376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}