はじめてのDjangoアプリ作成(その1)

参考サイトにしたがって作業した時の自分的メモ。

[ やること ]
右も左も分からないエンジニアがDjangoの手習いをする。

[ 関連エントリ ]
その1 : プロジェクト作成 & モデル作成
その2 : admin サイトの使い方
その3 : ビュー & テンプレートを作る(照会画面)
その4 : フォームを作る(入力画面)

[ 参考サイト ]
Django ドキュメントはじめての Django アプリ作成、その 1

[ 前提としてあるといい知識 ]
Python
RDB
MVCの概念

[ 環境 ]
ローカルマシンにXAMPP1.73 & Python2.5 インストール済。
DBはMySQL
エディタはPyScripter。

[ プロジェクト名 ]
プロジェクト名は「mysite」。
アプリケーション名は「polls」。

[ プロジェクト内容 ]
質問内容と選択肢があり、各選択肢に投票できるシステム。

――――――――
■ プロジェクトの作成
(01) コマンドプロンプトにて、てきとーなフォルダに移動。

(02) 下記コマンド実行にて、プロジェクト作成。
    django-admin.py startproject [プロジェクト名]

(03) プロジェクトフォルダ & 4ファイルが作成されたことを確認。
    

■ 実行確認
(01) コマンドプロンプトにて、プロジェクトフォルダに移動。
    cd mysite

(02) 下記コマンド実行にて、Django開発サーバを起動。
    python manage.py runserver

(03) コマンドプロンプトに、下記メッセージが表示されたら、サーバ起動成功。
    Validating models…
    0 errors found
    Django version 1.1.1, using settings ‘mysite.settings’
    Development server is running at http://127.0.0.1:8000/
    Quit the server with CTRL-BREAK.

(04) ブラウザにて「http://127.0.0.1:8000/」にアクセスして、下記ページが表示されることを確認。
    

■ Databaseの設定

(01) ファイル「settings.py」を編集。

DATABASE_ENGINE='mysql'
DATABASE_NAME='db_mysite'
DATABASE_USER='xxx'
DATABASE_PASSWORD='xxx'
DATABASE_HOST=''
DATABASE_PORT='3306'

(02) 下記コマンド実行にて、テーブル作成(DB同期)
    python manage.py syncdb

(03) コマンドプロンプトに下記メッセージが表示される。スーパーユーザを作るため、名前・メール・パスワードを入力。
    D:\mysite>python manage.py syncdb
    Creating table auth_permission
    Creating table auth_group
    Creating table auth_user
    Creating table auth_message
    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
    Username: xxx
    E-mail address: mail@domain.com
    Password:
    Password (again):
    Superuser created successfully.
    Installing index for auth.Permission model
    Installing index for auth.Message model

(04) コマンドプロンプトにて、DBにテーブルが作成されたことを確認する。
    ※ 実は一度エラーが発生してテーブル作成に失敗したけど、単純リランでうまくいった。
    D:\>mysql -u xxx -p db_mysite
    Enter password: *****
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 12
    Server version: 5.1.41 Source distribution
    
    show tables;

    +—————————-+
    | Tables_in_db_mysite
    +—————————-+
    | auth_group
    | auth_group_permissions
    | auth_message
    | auth_permission
    | auth_user
    | auth_user_groups
    | auth_user_user_permissions
    | django_content_type
    | django_session
    | django_site
    +—————————-+
    10 rows in set (0.00 sec)

    ※ syncdbコマンドにより
      ファイル「settings.py」の下部、「INSTALLED_APPS」に記述されているアプリケーションのテーブルが作られる。

■ アプリケーション作成

(01) 下記コマンド実行にて、アプリケーション作成
    python manage.py startapp [アプリケーション名]

(02) プロジェクトフォルダ配下に、アプリケーションフォルダ & 4ファイルが作成されたことを確認。
    

■ モデルの作成

(01) ファイル「models.py」を編集。

from django.db import models
 
class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
 
class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

(02) ファイル「settings.py」を編集。

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'mysite.polls' # ★ 追記
)

    ※ 「INSTALLED_APPS」への記述により、「models.py」の内容が、syncdbコマンドの対象に含まれた。

(03) 下記コマンド実行にて、テーブル作成(DB同期)
    python manage.py syncdb

(04) コマンドプロンプトに下記メッセージが表示される。
    Creating table polls_poll
    Creating table polls_choice
    Installing index for polls.Choice model

(05) コマンドプロンプトにて、DBにテーブルが作成されたことを確認できた。
    
    show tables;

    +—————————-+
    | Tables_in_db_mysite
    +—————————-+
    | auth_group
    | auth_group_permissions
    | auth_message
    | auth_permission
    | auth_user
    | auth_user_groups
    | auth_user_user_permissions
    | django_content_type
    | django_session
    | django_site
    | polls_choice
    | polls_poll
    +—————————-
    12 rows in set (0.02 sec)
    
    show fields from polls_choice;

    +———+————–+
    | Field | Type
    +———+————–+
    | id   | int(11)
    | poll_id  | int(11)
    | choice | varchar(200)
    | votes | int(11)
    +———+————–+
    
    describe polls_poll;

    +———-+————–+
    | Field | Type
    +———-+————–+
    | id    | int(11)
    | question | varchar(200)
    | pub_date | datetime
    +———-+————–+
    

    ※ テーブル構成の確認は「show fields」「describe」のどちらでも可。