Pages

Tuesday, December 4, 2012

Django:実践チュートリアル1(プロジェクトの作成とDB連携)

作成したいプロジェクト
とりあえず、実用的な機能を網羅したいので、まずは、PersonとAddressを管理できるシステムを作りたいと思います。
Personにはとりあえず、名前、メールアドレス、そして、顔写真の管理をする。
AddressはPersonに紐付けられており、住所が記載できるようにと考えています。
今後のチュートリアルでは都道府県などを結びつけるなども出来ればと思っています。

プロジェクトをまずは作成してみます。とりあえず、
私の開発環境は

OS: Mountain Lion
Pythonバージョン: Python 2.7.2

まずは、フォルダを以下の用に作り、/programmingpapa/code
以下のコマンドでプロジェクトを作成します。
$ django-admin.py startproject papatutorial

以下の構造でプロジェクトが作成されているはず。
papaturtorial - manage.py
                       papatutorial - __init__.py
                                           - settings.py
                                           - urls.py
                                           - wsgi.py
 
データベースをMySqlに接続するように設定を変更します。
settings.pyの以下の部分を変更します。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

私の環境ではmysqlで, データベースを'papatutorialdb'd, rootユーザー、パスワード無しなので以下のようになります。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'papatutorialdb',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '127.0.0.1',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}
注目していただきたい変更点はHOST です。デフォルトではlocalhostと書かれていますが、
次に出てくるデータベースとの連携の部分で私の環境ではエラーが出てきたので127.0.0.1にHOSTをしておくことで、問題が回避されました。

それでは、papatutorialフォルダ(/programmingpapa/code/papatutorial)に移りデータベースをシンクロさせましょう。

$ ./manage.py syncdb 
するとはじめてなので、以下の様な結果が帰ってきます。

Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
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と書いてEnter
 ユーザーネーム、Email,そして、パスワードを順に聞かれていくので、記入していきます。
終わると、phpMyAdminか何かで、papatutorialdbに色々テーブルができていることが確認できます。以下のテーブルができていました。

 auth_group
 auth_group_permissions
 auth_permission
 auth_user
 auth_user_groups
 auth_user_user_permissions
 django_content_type
 django_session
 django_site

ちなみに、auth_userテーブルを覗くと、作成したユーザーがあるはず。

No comments:

Post a Comment