apilecture for 2014/02/22 at shannonlab
DESCRIPTION
lecture resume for 2/22TRANSCRIPT
![Page 1: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/1.jpg)
Django×API で遊ぶ
小林泰
![Page 2: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/2.jpg)
タイムライン0. 自己紹介1 . API とは?2 . API の(具体的)使い方例3 .データ形式4. Python のターミナル上で動かしてみる5. Django にのせてみる
![Page 3: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/3.jpg)
1 . API とは?
![Page 4: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/4.jpg)
API の定義
Application programming Interface
直訳:アプリケーションをプログラムする時の接続
![Page 5: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/5.jpg)
API がなければHello world! と表示されるためのプログラムでも
①Hello world という文字のドットデータを作る② そのデータをバッファに格納するプログラムを作る③ 格納されたデータが正しく出力されるようにグラフィックカー ドに対するプログラムも作る
めんどくさい。
![Page 6: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/6.jpg)
WEB API
今回扱うのは WEB API と呼ばれるもの
「 Http リクエストを送ったら、データを返してくれるようなもの」
![Page 7: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/7.jpg)
ためしに。。。。今日使う API をブラウザーで叩いてみよう参照ホームページ http://zip.cgis.biz
![Page 8: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/8.jpg)
How to Use
![Page 9: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/9.jpg)
How to use
http://zip.cgis.biz/xml/zip.php?zn=郵便番号というページをリクエストすると住所のデータが xml 形式で返ってくる
Cf : http://zip.cgis.biz/csv/zip.php?zn=郵便番号
![Page 10: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/10.jpg)
TRY 1① ブラウザーを開く
![Page 11: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/11.jpg)
TRY 2http://zip.cgis.biz/xml/zip.php?zn=郵便番号で検索
![Page 12: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/12.jpg)
TRY3
結果が返される
![Page 13: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/13.jpg)
結果 xml 形式<ZIP_result><result name="ZipSearchXML"/><result version="1.01"/><result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D1690075"/><result request_zip_num="1690075"/><result request_zip_version="none"/><result result_code="1"/><result result_zip_num="1690075"/><result result_zip_version="0"/><result result_values_count="1"/><ADDRESS_value><value state_kana=" トウキョウト "/><value city_kana=" シンジュクク "/><value address_kana=" タカダノババ "/><value company_kana="none"/><value state=" 東京都 "/><value city=" 新宿区 "/><value address=" 高田馬場 "/><value company="none"/></ADDRESS_value></ZIP_result>
![Page 14: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/14.jpg)
csv の方もやってみようhttp://zip.cgis.biz/csv/zip.php?zn= 郵便番号
![Page 15: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/15.jpg)
結果 csv 形式"ZipSearchXML","1.01","http://zip.cgis.biz/csv/zip.php?zn=1690075","1690075","none","1","1690075","0","1"" トウキョウト "," シンジュクク "," タカダノババ ","none"," 東京都 "," 新宿区 "," 高田馬場 ","none"
![Page 16: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/16.jpg)
WEB API
今回扱うのは WEB API と呼ばれるもの
「 Http リクエストを送ったら、データを返してくれるようなもの」
例) http://zip.cgis.biz/csv/zip.php?zn=1690075にアクセス
☞住所が csv 形式で得られる!!
![Page 17: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/17.jpg)
2 . Examples of APIs
![Page 18: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/18.jpg)
WEB API 例Facebook api
![Page 19: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/19.jpg)
WEB API 例Twitter api
![Page 20: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/20.jpg)
twitterAPI を使っている例
![Page 21: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/21.jpg)
3 .得られるデータ
![Page 22: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/22.jpg)
得られるデータxmlcsvjsonExel シート
等が得られる
![Page 23: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/23.jpg)
xml
Extensible Markup Languageタグを用いて論理構造などを表現
< 要素名 属性 =" 値 "> 内容 </ 要素名 >
![Page 24: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/24.jpg)
Xml 例:さっきのやつ<ZIP_result><result name="ZipSearchXML"/><result version="1.01"/><result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D1690075"/><result request_zip_num="1690075"/><result request_zip_version="none"/><result result_code="1"/><result result_zip_num="1690075"/><result result_zip_version="0"/><result result_values_count="1"/><ADDRESS_value><value state_kana="トウキョウト"/><value city_kana="シンジュクク"/><value address_kana="タカダノババ"/><value company_kana="none"/><value state="東京都"/><value city="新宿区"/><value address="高田馬場"/><value company="none"/></ADDRESS_value></ZIP_result>
![Page 25: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/25.jpg)
csv
Comma-separated values字義通り、カンマで値を区切る形式
例:さっきのやつ" トウキョウト "," シンジュクク "," タカダノババ ","none"," 東京都 "," 新宿区 "," 高田馬場 "
![Page 26: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/26.jpg)
json
JavaScript Object NotationJavascript のオブジェクト記述をベースにしている
{"name": "John Smith", "age": 33}
☞python の辞書形式にかなり近い
![Page 27: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/27.jpg)
Json を返してくれる API の例http://www.ekidata.jp/api/api_line.php
![Page 28: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/28.jpg)
加工
データそのままでは使えないので、それを切り出す操作が必要になる。 →それを行うためのモジュールがある
![Page 29: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/29.jpg)
Xml の場合・ Element Tree ( python 標準装備)・ xml.dom.minidom・ lxml・ Beautifulsoup・ xml.parsers.expatネットにいっぱい落ちています。
![Page 30: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/30.jpg)
Element Tree
Xml データそのままでは使えないので、 pythonで使えるように変更する必要がある。Element 型という形
例:import xml.etree.ElementTree as ETdata = ET.fromstring (response.read())
![Page 31: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/31.jpg)
Practice:element tree
<window width="1920"><title font="large"
color="red">sample</title><buttonbox>
<button>OK</button><button>Cancel</button>
</buttonbox></window>
![Page 32: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/32.jpg)
Step1: 文字列からString = <window width="1920”><title font="large" color="red">sample</title><buttonbox><button>OK</button><button>Cancel</button></buttonbox></window>
elem = fromstring(String)
![Page 33: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/33.jpg)
Step1: ファイルからdata = parse(“sample.xml”)elem = data.getroot()
![Page 34: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/34.jpg)
elem ← この中にデータを格納した!! 取り出してみよう
![Page 35: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/35.jpg)
tag 名<window width="1920">
<title font="large" color="red">sample</title>
<buttonbox><button>OK</button><button>Cancel</button>
</buttonbox></window>
![Page 36: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/36.jpg)
attribute
<window width="1920"><title font="large"
color="red">sample</title><buttonbox>
<button>OK</button><button>Cancel</button>
</buttonbox></window>
![Page 37: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/37.jpg)
Attribute の value
<window width="1920"><title font="large"
color="red">sample</title><buttonbox>
<button>OK</button><button>Cancel</button>
</buttonbox></window>
![Page 38: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/38.jpg)
value
<window width="1920"><title font="large"
color="red">sample</title><buttonbox>
<button>OK</button><button>Cancel</button>
</buttonbox></window>
![Page 39: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/39.jpg)
Tag の名前の取り出しprint elem.tag
Result:window
![Page 40: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/40.jpg)
Attribute を指定して value 取得print elem.get(“width”)
Result:1920
![Page 41: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/41.jpg)
Attribute 名のリスト取得print elem.keys()
Result:[‘width’]
![Page 42: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/42.jpg)
どっちも欲しい!!print elem.items()
Result:[(‘width’,’1920’)]
![Page 43: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/43.jpg)
条件にマッチする要素を返すfor e in elem.findall(“.//button”): print e.tag
Result:buttonbutton
![Page 44: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/44.jpg)
全部ほしいfor e in elem.getiterator(): print e.tag
Result:windowtitlebuttonboxbuttonbutton
![Page 45: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/45.jpg)
特定のものだけ欲しいfor in elem.getiterator(“button”):
print e.tag
Result:buttonbutton
![Page 46: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/46.jpg)
![Page 47: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/47.jpg)
csv
csv (という標準装備のモジュールがある)
例 :reader さえ使えればなんとかなるlist =[]f = csv.reader(FILE)for row in f:
list.append(row)print list
![Page 48: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/48.jpg)
json
json (標準装備)Simplejson
例>>> import json>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]>>> json.loads('"\\"foo\\bar"')u'"foo\x08ar’
![Page 49: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/49.jpg)
Practice( 変換だけですが )
{'Python':'python.org’, 'SearchEngine':('gogle.co.jp','yahoo.co.jp')}
これをデコードして辞書型に変換しましょう。デコードしたものから key ごとに取り出せるかも見てみましょう
![Page 50: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/50.jpg)
そのまえに encode してみるimport json
data = さっきのencoded = json.dumps(data)Print type(encoded)
![Page 51: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/51.jpg)
Decode してみようdecoded = json.loads(encoded)
print decodedprint type(decoded)
![Page 52: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/52.jpg)
取り出し
print decoded[‘Python’]print decoded[‘SearchEngine’]
![Page 53: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/53.jpg)
![Page 54: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/54.jpg)
3 .ターミナルで動かす
![Page 55: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/55.jpg)
郵便番号プログラム
さっきの郵便番号から住所を得る API を使うプログラムを書いて、結果を表示してみましょう。
![Page 56: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/56.jpg)
例題1:データを得てそのまま表示
手順:
①アクセスする url 文字列を作成 ② urllib2 モジュールで、作った url 文字列を使 用して、 http レスポンスを得る ③得たレスポンスを print で表示
![Page 57: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/57.jpg)
① アクセスする url を作るヒント:
・ input を使う・固定部分 url = “http://zip.cgis.biz/xml/zip.php?zn=“ 追加する部分(郵便番号)= input url = url + 郵便番号
※ 型は string
![Page 58: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/58.jpg)
②urllib2 でレスポンスを得るヒント:
レスポンスオブジェクトは
response = urllib2.urlopen(url)
で得られます
![Page 59: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/59.jpg)
③ レスポンスを表示ヒント:
オブジェクトprint response で ok
![Page 60: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/60.jpg)
例題1:解答例#!/usr/bin/env python# -*- coding: utf-8 -*-
import urllib2import xml.etree.ElementTree as ET
if __name__=='__main__':num = input('address number?')url = 'http://zip.cgis.biz/xml/zip.php?zn='address = str(num)response = urllib2.urlopen(url+address)
data = response.read()print data
![Page 61: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/61.jpg)
例題2:加工して表示得たデータを
都道府県 値市町村 値住所 値
という形に表示しよう。
![Page 62: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/62.jpg)
手順①response オブジェクトを加工可能な形に変換 → element.tree を使う② 変換したものを python 辞書型に落とし込む③ それを表示
![Page 63: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/63.jpg)
①response オブジェクトを加工ヒント
まずはパースすること→ elementtree 型にする
参考: http://docs.python.jp/2/library/xml.etree.elementtree.htmlあとは先ほどのスライド
![Page 64: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/64.jpg)
②python 辞書型に変換ヒント今回の郵便番号 API は value でなくてattributeとして結果を返している
<value state=" 東京都 "/>
赤字が欲しい!! elem.items ???
![Page 65: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/65.jpg)
③ 表示ヒント
辞書をプリントするので、キーを指定してプリントすれば ok
![Page 66: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/66.jpg)
解答例前半#!/usr/bin/env python# -*- coding: utf-8 -*-
import sysimport urllib2from xml.etree.ElementTree import *import xml.etree.ElementTree as ET
def api(a):url = 'http://zip.cgis.biz/xml/zip.php?zn='address = str(a)response = urllib2.urlopen(url+address)return response
![Page 67: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/67.jpg)
解答例後半if __name__=='__main__':
num = input('address number?')response = api(num)data = response.read()root = ET.fromstring(data)data = {}for value in root.iter('value'):for k,v in value.attrib.items():#print k , vnome = kbasho = vdata.update({nome:basho})
![Page 68: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/68.jpg)
<ZIP_result><result name="ZipSearchXML"/><result version="1.01"/><result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D1690075"/><result request_zip_num="1690075"/><result request_zip_version="none"/><result result_code="1"/><result result_zip_num="1690075"/><result result_zip_version="0"/><result result_values_count="1"/><ADDRESS_value><value state_kana=" トウキョウト "/><value city_kana=" シンジュクク "/><value address_kana=" タカダノババ "/><value company_kana="none"/><value state=" 東京都 "/><value city=" 新宿区 "/><value address=" 高田馬場 "/><value company="none"/></ADDRESS_value></ZIP_result>
![Page 69: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/69.jpg)
解答例最後 state = data['state']
city = data['city']address = data['address']print stateprint cityprint address
![Page 70: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/70.jpg)
4 . Django で動かす
![Page 71: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/71.jpg)
Django復習https://www.djangoproject.com
![Page 72: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/72.jpg)
Django とは『 python 用の web フレームワーク』
※ フレームワーク web アプリケーションの鋳型 根本的な部分はフレームワークに任せられる
![Page 73: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/73.jpg)
おおまかな構造
view
MODEL
URL
![Page 74: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/74.jpg)
おおまかな構造
view
MODEL
URL
HTTP リクエスト
![Page 75: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/75.jpg)
おおまかな構造
view
MODEL
URL
HTTP リクエスト
命令振り分け
![Page 76: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/76.jpg)
おおまかな構造
MODEL
URL
HTTP リクエスト
view処理!!
データやり取り
![Page 77: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/77.jpg)
おおまかな構造
view処理!!
Django
HTTP レスポンスオブジェクト
Result
![Page 78: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/78.jpg)
view
ページの表示にかかわる部分。アプリケーションの処理部分を書く
![Page 79: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/79.jpg)
MODEL
アプリケーションに欠かせないデータの保管に関係する部分
![Page 80: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/80.jpg)
URL
作成するアプリケーションの別々のページの間の関係、 URL まわりの設定をする
![Page 81: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/81.jpg)
Django プロジェクトを立ち上げる
ダウンロードしていない人はする
![Page 82: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/82.jpg)
startproject
適当なディレクトリでdjango-admin.py startproject mysite を実行
![Page 83: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/83.jpg)
ディレクトリができているはず
![Page 84: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/84.jpg)
自動的にディレクトリの中身も
![Page 85: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/85.jpg)
startapp するpython manage.py startapp finda
![Page 86: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/86.jpg)
自動的に finda が作成される
![Page 87: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/87.jpg)
データベースを使えるようにする
python manage.py syncdb
→ 上手く動かない。 settings.py を変更
![Page 88: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/88.jpg)
settings.py
![Page 89: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/89.jpg)
settings.py MODEL設定
![Page 90: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/90.jpg)
settings.py MODEL設定
![Page 91: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/91.jpg)
改めて syncdb
![Page 92: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/92.jpg)
foo ができている
![Page 93: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/93.jpg)
フローを考える《住所表示アプリケーション》① トップページにアクセス② トップページの郵便番号欄に打ち込む③ 送信④結果表示
![Page 94: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/94.jpg)
① トップページにアクセス機能・ top ページ url にリクエストを送ると、郵便番号 記入するフォームが現れる
・ view に書き込む index クラス
![Page 95: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/95.jpg)
②③ 送信機能・送信すると、 view 内の他の関数が働いて、入 力された数字を加工して、 api に送信、データ を受け取る
これも view に書く
![Page 96: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/96.jpg)
④結果表示機能・②③のデータ処理と同じところに入れるor・②③の結果をモデルに入れて、そこから引き 出して使う
サンプルプログラムは前者です
![Page 97: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/97.jpg)
① いきなりはわからない
![Page 98: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/98.jpg)
①
![Page 99: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/99.jpg)
①finda/urls.py
![Page 100: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/100.jpg)
①
![Page 101: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/101.jpg)
①mysite/urls.py
![Page 102: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/102.jpg)
①mysite/urls.py
![Page 103: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/103.jpg)
runserver
![Page 104: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/104.jpg)
結果
![Page 105: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/105.jpg)
template の利用
![Page 106: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/106.jpg)
finda/templates/finda/index.html
![Page 107: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/107.jpg)
view の書き換え
![Page 108: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/108.jpg)
template の読み込み
![Page 109: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/109.jpg)
結果
![Page 110: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/110.jpg)
template に値を入れて表示したい時
html{{listall}}
viewlist = listcontext = {‘listall’:list}return render(request,'finda/index.html',context)
![Page 111: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/111.jpg)
Django で動かすここまでで view の作り方がわかりました
・値は html の form で得るといいでしょう。・得たものを url 文字列に整形します 初めの方に作ったプログラムを流用します・先ほどのプログラムで辞書型データを作成・ template に値を与えて表示
![Page 112: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/112.jpg)
手順例① プロジェクトを作る 済②app を作る 済③syncdb 済④settings.py の設定 済⑤urls.py の設定 result クラスの url設定⑥ビューの作成 result クラスを作る⑦テンプレートの作成
![Page 113: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/113.jpg)
やってみましょう参考 url:チュートリアルhttps://docs.djangoproject.com/en/1.6/intro/tutorial01/posts 送信されたデータの受け取りhttp://www.djangoproject.jp/doc/ja/1.0/ref/request-response.htmlhttp://stackoverflow.com/questions/12518517/request-post-getsth-vs-request-poststh-difference
![Page 114: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/114.jpg)
やってみましょう
![Page 115: Apilecture for 2014/02/22 at shannonlab](https://reader038.vdocuments.pub/reader038/viewer/2022110119/5565fc1cd8b42a20158b5529/html5/thumbnails/115.jpg)
解答
下のコマンドでgitしてください
git clone https://bitbucket.org/tsukukobaan/api.git/