ちょっと戻って

こっちのページのソースを入れて

C:\Program Files\Google\google_appengine>dev_appserver.py C:\Python25\proj\appengine_helper_for_django-r52

 で django の起動には成功。

> http://localhost:8080/_ah/admin/datastore

 はうまくいったが、

> http://localhost:8080/polls/

 は、失敗。

ImportError at /polls/
No module named urls
Request Method: GET
Request URL: http://localhost:8080/polls/
Exception Type: ImportError
Exception Value: No module named urls
Exception Location: C:\Program Files\Google\google_appengine\lib\django\django\core\urlresolvers.py in _get_urlconf_module, line 177

 ちなみに、でたらめなページを指定すると

Page not found (404)
Request Method: GET
Request URL: http://localhost:8080/detarame/

Using the URLconf defined in urls, Django tried these URL patterns, in this order:

1. ^polls/
2. ^shelf/

The current URL, /detarame/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

 と表示されるので、動いていることは動いているらしい。

        • -

 いったんシェルに戻って試す。

C:\Python25\proj\appengine_helper_for_django-r52>python manage.py shell
WARNING:root:Could not initialize images API; you are likely missing the Python
"PIL" module. ImportError: No module named PIL
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

# PIL ってなに?

最新Pythonエクスプローラ pp12 の図2を参考にメンバー変数にアクセスすると、get メソッドが動いていない模様。

>>> from polls.models import Poll
>>> poll1 = Poll(question="987")
>>> poll1.save()
datastore_types.Key.from_path('Poll', 5L, _app=u'google-app-engine-django')
>>> Poll.objects.all()

>>> poll2 = Poll.objects.get(pk=1)
Traceback (most recent call last):
File "", line 1, in
TypeError: get() got an unexpected keyword argument 'pk'

 いろいろ調べては見たが、よく分からない。高速道路はまだ通っていないようだ。

An example of using Django on top of App Engineは datastore を直接アクセスする必要がありそうだし...。

Django 管理インターフェイスが出る

 昨日躓いた原因は、settings.py の編集もれがあったこと。それを直したあと、トントンと進んだが、認証時のユーザー名を思い出せずにしばらく時間がかかる。

 ユーザー名を思い出し、管理画面が出たところで今日は終り。続きは明日。

psycopg と postgreSQL 8.3.3

Python エクスプローラーDjango 01 のサンプルソースを動かそうと python manage.py syncdb を実行するとうまくいかない。

C:\Python25\proj\djbookshelf>python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 11, in
execute_manager(settings)
File "C:\python25\Lib\site-packages\django\core\management.py", line 1672, in
execute_manager
execute_from_command_line(action_mapping, argv)
File "C:\python25\Lib\site-packages\django\core\management.py", line 1571, in
execute_from_command_line
action_mapping[action](int(options.verbosity), options.interactive)
File "C:\python25\Lib\site-packages\django\core\management.py", line 504, in s
yncdb
cursor = connection.cursor()
File "C:\python25\Lib\site-packages\django\db\backends\postgresql_psycopg2\bas
e.py", line 57, in cursor
postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].spli
t('.')]
ValueError: invalid literal for int() with base 10: '3,'

ValueError: invalid literal for int() postgresql_psycopg2 で 検索するとこんなページが

 面倒くさいので 58 行目を変更

# postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')]
postgres_version = ['8','3','3']

 これで動くかといえば、syncdb は成功したが、そのさきで引っかかる。先は長そうだ。

Google-app-engine と django

 django を調べていると、対応しているデータベースはメジャーどころばかり。google app engine の提供するデータベースには対応していないのだろうか?Getting Started の Using the webapp Framework には Django の名前があるのだが...。

 更に調べると、こんなページが。簡単にはいかないか。

 

Amazon の表紙イメージを持ってくる

 このビデオこの資料、それとこのページや各種オンラインドキュメントを参考に、ISBN を入力すると、ブラウザを開いて Amazon に登録されている表紙イメージを開くプログラムを作る。

 文字列の tag を落とすのに手間取ったが、それでも2時間ほど。もっと「エレガントな解」がありそうだが、それはまたそのうち。イテレーターってのは、慣れれば便利なんだろうなぁ。
 
 次の目標は Django と データベース。

import urllib
import BeautifulSoup
import webbrowser
d = {
'Service': 'AWSECommerceService',
'AWSAccessKeyId': '0EAWZ7V3N3WQWXK27Z02',
'Operation': 'ItemSearch',
'SearchIndex': 'Books',
'Keywords': '409182160X',
'ResponseGroup':'Images'
}

s = raw_input('ISBN-number')
d['Keywords'] = s
uri = 'http://webservices.amazon.co.jp/onca/xml?' + urllib.urlencode(d)
f = urllib.urlopen(uri)
response = f.read()
bs = BeautifulSoup.BeautifulSoup(response)
#fi = file("xx.xml","w")
#fi.write(bs.contents[0].string)
#fi.write(str(bs))
#print str(bs)
#fi.close()
b1 = bs.find('largeimage')
s2 = str(b1.find('url'))
s3 = ''
for i in range(5,len(s2) - 6):
s3 = s3 + s2[i]
print s3
webbrowser.open(s3)