Django

Googleでも利用されているフレームワークです。こりゃ覚えておかないと。

環境

http://www.djangoproject.com/からダウンロードしました。

tar xzvf Django-0.96.1.tar.gz
cd Django-0.96.1
python setup.py install

leopardの場合、django-admin.pyは/usr/local/binにいました。windowsでは

C:\python25;C:\python25\Scripts;

にでもパスを通しておきましょう。 ではPython DB API-2.0インターフェイスであるMySQLdbも入れておきましょう。 http://sourceforge.net/projects/mysql-pythonより

wget http://downloads.sourceforge.net/mysql-python/MySQL-python-1.2.2.tar.gz?modtime=1172959928&big_mirror=0

windowsはMySQL-python-1.2.2.win32-py2.5.exeで実行すれば、OK

tar xzvf MySQL-python-1.2.2.tar.gz 
python setup.py build

んーエラーが

sh: mysql_config: command not found

ではsite.cfgを編集して、

mysql_config = /usr/local/mysql5/bin/mysql_config 

としてもう一度。

In file included from /usr/local/mysql5/include/mysql/mysql.h:47,
                 from _mysql.c:40:
/usr/include/sys/types.h:92: error: duplicate 'unsigned'
/usr/include/sys/types.h:92: error: two or more data types in declaration  specifiers
In file included from /usr/local/mysql5/include/mysql/mysql.h:47,
                from _mysql.c:40:
/usr/include/sys/types.h:92: error: duplicate 'unsigned'
/usr/include/sys/types.h:92: error: two or more data types in declaration specifiers
lipo: can't open input file: /var/tmp//ccGIog3v.out (No such file or directory)
error: command 'gcc' failed with exit status 1

んーこりゃこまった。_mysql.cを開いて

#ifndef uint                                                                                        
#define uint unsigned int                                                                           
#endif                                                                                              

をコメントしもう一度

ld: warning in /usr/local/mysql5/lib/mysql/libmysqlclient_r.dylib, file is not  of required architecture

あっそ。leopardはだめなの?ワーニングなんでとりあえず進もう。

python setup.py install

では確認

python
>>>import MySQLdb

でエラーはでなかった。 参考:Problems in building 1.2.2 on Mac OS 10.5

次はPostgresqlもってことで、http://initd.org/software/initd/psycopgから、ん?つながらん。ではhttp://www.zope.org/Members/fog/psycopgから

wget http://www.zope.org/Members/fog/psycopg/1.1.6/psycopg-1.1.6.tar.gz
tar xzvf psycopg-1.1.6.tar.gz 

あっつながった.... 2がでてるやんか。windows用はhttp://www.stickpeople.com/projects/python/psycopg/にありました。psycopg2-2.0.7.win32-py2.5-pg8.3.1-release.exeをダウンロードして、実行です。

wget http://initd.org/pub/software/psycopg/psycopg2-2.0.6.tar.gz 
tar xzvf psycopg2-2.0.6.tar.gz 
cd psycopg2-2.0.6
python setup.py build

おっまたエラーだ。

warning: /bin/sh: pg_config: command not found

./psycopg/connection.h:27:22: error: libpq-fe.h: No such file or directory

がでてるぞ。ではsetup.cfgに

pg_config=/usr/local/pgsql/bin/pg_config

としてやる。おっ動いた。ただワーニングだけど

ld: warning in /usr/local/pgsql/lib/libpq.dylib, file is not of required architecture

ってでたな。mysqlと同じか。とりあえず進もう。

python setup.py install

確認として

python
>>>import psycopg2 

でエラーはでなかった。

作る

早速プロジェクトをつくりましょう。ターミナルから

django-admin.py startproject TestDjango

次にEclispeからPyDev?(Eclipseプラグイン)プロジェクトを選択し、 プロジェクト名をdjango-admin.pyでつけた、TestDjango?、 「デフォルト'src'フォルダーを作成し、pythonpathへ追加」のチェックを外して作成します。
作成ができましたら、今度はそのディレクトリで

manage.py startapp testapp

でひな形を作成します。
できましたら、eclipseのTestDjango?上で右クリックして、プロパティ>PyDev?-PYTHONPATH >ソースフォルダの追加でTestDjango?をPYTHONPATHに追加します。
testappにmodels.pyができてますんで、

class Book(models.Model):
   isbn = models.CharField(blank=True,maxlength=100)
   author = models.CharField(blank=False,maxlength=100)
   title = models.CharField(blank=False,maxlength=100)
   price = models.FloatField(max_digits=8,decimal_places=2)

とかでつくっておきます。
settings.pyも直しておきましょう。対象のdbとかにあわせて

DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'hotedb'
DATABASE_USER = 'root'
DATABASE_PASSWORD = 'hogepass'
DATABASE_HOST = ''
DATABASE_PORT = ''
TIME_ZONE = 'Japan'
LANGUAGE_CODE = 'ja'
INSTALLED_APPS = (
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'django.contrib.admin',
   'TestDjango.testapp',
)

ではアプリのインストールです。

c:\MyProject\python\TestDjango>manage.py syncdb
Creating table auth_message
Creating table auth_group
Creating table auth_user
Creating table auth_permission
Creating table django_content_type
Creating table django_session
Creating table django_site 
Creating table testapp_book

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes <<
Username: admin <<
E-mail address: hoge@hoge.com <<
Password: hogepass <<
Password (again): hogepass <<
Superuser created successfully.
Installing index for auth.Message model
Installing index for auth.Permission model
Loading 'initial_data' fixtures...
No fixtures found.

ではちょっと動かしてみましょう。

python manage.py runserver

Eclipseから動かす時は、一度manage.pyをデバッグのPython実行で動かしておき、 失敗するので、デバッグダイアログを開くと、さきほどの動かした設定が追加されているので、引数に

runserver 8080 --noreload

を追加して再度、デバッグのPython実行をします。 他のマシンから受け付けるときは

python manage.py runserver 0.0.0.0:8000

それではブラウザから

http://localhost:8000/

manage.py

アプリケーション雛形作成

python manage.py startapp アプリケーション

テーブル作成

settings.pyのINSTALLED_APPSに記述したアプリケーションのテーブルを作成します。

python manage.py syncdb

SQL表示

CREATE TABLEを表示してくれます。

python manage.py sql アプリケーション

SQL表示 すべて

CREATE TABLEだけでなくCREATE INDEXも含めて表示してくれます。

python manage.py sqlall アプリケーション

SQL表示 INDEXのみ

python manage.py sqlindexes アプリケーション

ターミナルから触ってみる

python manage.py shell

後はfrom TestDjango?.testapp.models import Bookとかでインポートしたりして試してみましょう。

TIPS

__str__

javaでtoStringか。

TemplateDoesNotExist? at /admin/

Leopardなんですが、adminを動かそうとするとエラーがでました。

cp -R django/contrib/admin/media /Library/Python/2.5/site-packages/django/contrib/admin
cp -R django/contrib/admin/template /Library/Python/2.5/site-packages/django/contrib/admin

とりあえずこれで動いた

日本語を表示したい

ソースの先頭に

# -*- coding: utf-8 -*-

を書く

リンク

Djangoとは
PyDevでDjangoのアプリをデバッグする
Djangoを使ってみる - DBを使って住所録的なモノを

参考書籍

コメント

  • #comment

トップ   編集 凍結解除 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2008-07-25 (金) 10:51:00 (5769d)