Python Django 入門トップページ


Django によるコメント掲示板の開発:目次

  1. プロジェクトの作成
  2. Git でコミット
  3. Web サーバを起動しておく
  4. Config
  5. Comments アプリケーションを作る
  6. アプリケーションを有効にする
  7. はじめてのビューを作成する
  8. モデルを作る
  9. マイグレーション
  10. テストデータの設定
  11. データベースからコメント一覧を取得して表示してみよう
  12. Bootstrap の導入
  13. コメントの詳細表示
  14. urls.pyの書き方
  15. HTML のテンプレート化
  16. コメントの新規投稿
  17. コメントの編集機能を追加する
  18. さらにテンプレート化
  19. モデル,マイグレーションファイル,フォームの関連
  20. 入力内容の検証(バリデーション)
  21. コメントを削除する
  22. 一覧を逆順にする
  23. ページネーション
  24. フラッシュメッセージ
  25. Static コンテンツの設置
  26. 更新や削除にもフラッシュメッセージを表示
  27. テストの自動化を実現しよう
  28. デバッグツールバーを使う
  29. generic モジュールを使わずにコメント一覧を取得する
  30. コメント一覧のページネーション
  31. 一覧の表示順序を制御する
  32. generic モジュールを使わずにコメントの詳細を表示する
  33. コメント詳細にページ送り機能を作成する
  34. generic モジュールを使わずにコメント登録機能を作成する
  35. generic モジュールを使わずにコメント編集機能を作成する
  36. generic モジュールを使わずにコメント削除機能を作成する

Django によるコメント掲示板の開発

テストデータの設定

コメントの一覧表示をする前に,まずテストデータをデータベースに登録しよう.まず,comments ディレクトリに fixtures という名前のディレクトリを作成し,その中に comments-data.json ファイルを設置する.この中に適当なデータを数件登録すれば良い.今回は10件のデータを登録し,created_at(登録日時)が順番どおりになるようにしている.また,updated_at(更新日時)は9番目以外は created_at と同じで,9番目は10番目のコメントが投稿された後に更新されたことにしている.また,3個目のコメントにはHTMLのタグを入力してる.

comments/fixtures/comments-data.json[
    {
        "model": "comments.comment",
        "fields": {
            "title": "最初のコメント",
            "body": "コメントの本文",
            "created_at": "2022-07-27T11:01:00.000",
            "updated_at": "2022-07-27T11:01:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "2個目のコメント",
            "body": "コメントの本文2",
            "created_at": "2022-07-27T11:02:00.000",
            "updated_at": "2022-07-27T11:02:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "<3個目>のコメント",
            "body": "<h1>コメントの本文3</h1>",
            "created_at": "2022-07-27T11:03:00.000",
            "updated_at": "2022-07-27T11:03:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "4個目のコメント",
            "body": "コメントの本文4",
            "created_at": "2022-07-27T11:04:00.000",
            "updated_at": "2022-07-27T11:04:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "5個目のコメント",
            "body": "コメントの本文5",
            "created_at": "2022-07-27T11:05:00.000",
            "updated_at": "2022-07-27T11:05:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "6個目のコメント",
            "body": "コメントの本文6",
            "created_at": "2022-07-27T11:06:00.000",
            "updated_at": "2022-07-27T11:06:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "7個目のコメント",
            "body": "コメントの本文7",
            "created_at": "2022-07-27T11:07:00.000",
            "updated_at": "2022-07-27T11:07:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "8個目のコメント",
            "body": "コメントの本文8",
            "created_at": "2022-07-27T11:08:00.000",
            "updated_at": "2022-07-27T11:08:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "9個目のコメント",
            "body": "コメントの本文9",
            "created_at": "2022-07-27T11:09:00.000",
            "updated_at": "2022-07-27T11:20:00.000"
        }
    },
    {
        "model": "comments.comment",
        "fields": {
            "title": "10個目のコメント",
            "body": "コメントの本文10",
            "created_at": "2022-07-27T11:10:00.000",
            "updated_at": "2022-07-27T11:10:00.000"
        }
    }
]

テスト用データ(fixtures)の準備ができたら,実際にデータベースに登録してみよう.

(py39) C:\Users\lecture\Documents\django\django_comment>python manage.py loaddata comments\fixtures\comments-data.json ⏎
Installed 10 object(s) from 1 fixture(s)

(py39) C:\Users\lecture\Documents\django\django_comment>

登録されたことを sqlite3 で確認する.次のとおり実行するとデータベースのテーブルに保存されたデータを一覧で表示することができる.しかし,最初の SELECT 文では列名が表示されないことに注意する.列名もあわせて表示したい場合は,あらかじめ .headers ON を実行してから SELECT するとよい.

(py39) C:\Users\lecture\Documents\django\django_comment>sqlite3 db.sqlite3 ⏎
SQLite version 3.38.2 2022-03-26 13:51:10
Enter ".help" for usage hints.
sqlite> .tables ⏎
auth_group                  comments_comment
auth_group_permissions      django_admin_log
auth_permission             django_content_type
auth_user                   django_migrations
auth_user_groups            django_session
auth_user_user_permissions
sqlite> select * from comments_comment; ⏎
1|最初のコメント|コメントの本文|2022-07-27 11:01:00|2022-07-27 11:01:00
2|2個目のコメント|コメントの本文2|2022-07-27 11:02:00|2022-07-27 11:02:00
3|<3個目>のコメント|<h1>コメントの本文3</h1>|2022-07-27 11:03:00|2022-07-27 11:03:00
4|4個目のコメント|コメントの本文4|2022-07-27 11:04:00|2022-07-27 11:04:00
5|5個目のコメント|コメントの本文5|2022-07-27 11:05:00|2022-07-27 11:05:00
6|6個目のコメント|コメントの本文6|2022-07-27 11:06:00|2022-07-27 11:06:00
7|7個目のコメント|コメントの本文7|2022-07-27 11:07:00|2022-07-27 11:07:00
8|8個目のコメント|コメントの本文8|2022-07-27 11:08:00|2022-07-27 11:08:00
9|9個目のコメント|コメントの本文9|2022-07-27 11:09:00|2022-07-27 11:20:00
10|10個目のコメント|コメントの本文10|2022-07-27 11:10:00|2022-07-27 11:10:00
sqlite> .headers ON ⏎
sqlite> select * from comments_comment; ⏎
id|title|body|created_at|updated_at
1|最初のコメント|コメントの本文|2022-07-27 11:01:00|2022-07-27 11:01:00
2|2個目のコメント|コメントの本文2|2022-07-27 11:02:00|2022-07-27 11:02:00
3|<3個目>のコメント|<h1>コメントの本文3</h1>|2022-07-27 11:03:00|2022-07-27 11:03:00
4|4個目のコメント|コメントの本文4|2022-07-27 11:04:00|2022-07-27 11:04:00
5|5個目のコメント|コメントの本文5|2022-07-27 11:05:00|2022-07-27 11:05:00
6|6個目のコメント|コメントの本文6|2022-07-27 11:06:00|2022-07-27 11:06:00
7|7個目のコメント|コメントの本文7|2022-07-27 11:07:00|2022-07-27 11:07:00
8|8個目のコメント|コメントの本文8|2022-07-27 11:08:00|2022-07-27 11:08:00
9|9個目のコメント|コメントの本文9|2022-07-27 11:09:00|2022-07-27 11:20:00
10|10個目のコメント|コメントの本文10|2022-07-27 11:10:00|2022-07-27 11:10:00
sqlite> .exit ⏎

(py39) C:\Users\lecture\Documents\django\django_comment>

なお,python manage.py loaddata comments/fixtures/comments-data.json のコマンドを再度実行すると2重にデータが登録されてしまうことになる.以降で作成する投稿や更新機能を試す際には,データベースをロールバックして,マイグレーションによるテーブルの再生成,データの登録を順に行えば良い.

(py39) C:\Users\lecture\Documents\django\django_comment>python manage.py migrate comments zero ⏎
Operations to perform:
  Unapply all migrations: comments
Running migrations:
  Rendering model states... DONE
  Unapplying comments.0001_initial... OK

(py39) C:\Users\lecture\Documents\django\django_comment>python manage.py migrate ⏎
Operations to perform:
  Apply all migrations: admin, auth, comments, contenttypes, sessions
Running migrations:
  Applying comments.0001_initial... OK

(py39) C:\Users\lecture\Documents\django\django_comment>python manage.py loaddata comments\fixtures\comments-data.json ⏎
Installed 10 object(s) from 1 fixture(s)

(py39) C:\Users\lecture\Documents\django\django_comment>

Windows であれば上の3つのコマンドを & でつなげれば,一気に3つのコマンドを実行できる.(次のコマンドは改行して表示されていますが改行せずに入力してください)

(py39) C:\Users\lecture\Documents\django\django_comment>python manage.py migrate comments zero & python manage.py migrate & python manage.py loaddata comments\fixtures\comments-data.json ⏎
Operations to perform:
  Unapply all migrations: comments
Running migrations:
  Rendering model states... DONE
  Unapplying comments.0001_initial... OK
Operations to perform:
  Apply all migrations: admin, auth, comments, contenttypes, sessions
Running migrations:
  Applying comments.0001_initial... OK
Installed 10 object(s) from 1 fixture(s)

(py39) C:\Users\lecture\Documents\django\django_comment>

また,Mac や Linux ではコマンドをセミコロン (;) で繋げば良い.さらに history 1 コマンドから過去に入力したコマンドを探し,!番号 で再実行すると簡単である.(残念ながら Windows には history コマンドはありません.)なお,以前は history コマンドで過去に入力した500件(または1000件)+α のコマンドがすべて表示されていましたが,いつからか最新の16件しか表示されなくなりました.それよりも遡りたい場合は history 1 のように表示したい先頭番号を指定してください.また,!番号 を入力したあとに tab キーを押すと,実行する前にコマンドの内容を確認したり編集したりできるようになります.(この機能が使えるかどうかもシェルの種類?バージョン?によります.)

rinsaka@Macmini2020 django_comment % python manage.py migrate comments zero; python manage.py migrate; python manage.py loaddata comments/fixtures/comments-data.json ⏎
Operations to perform:
  Unapply all migrations: comments
Running migrations:
  Rendering model states... DONE
  Unapplying comments.0001_initial... OK
Operations to perform:
  Apply all migrations: admin, auth, comments, contenttypes, sessions
Running migrations:
  Applying comments.0001_initial... OK
Installed 10 object(s) from 1 fixture(s)
rinsaka@Macmini2020 django_comment %
rinsaka@Macmini2020 django_comment % history 1 | grep loaddata ⏎
 1022  python manage.py loaddata comments/fixtures/comments-data.json
 1051  python manage.py migrate comments zero; python manage.py migrate; python manage.py loaddata comments/fixtures/comments-data.json
rinsaka@Macmini2020 django_comment % !1051 ⏎
python manage.py migrate comments zero; python manage.py migrate; python manage.py loaddata comments/fixtures/comments-data.json
Operations to perform:
  Unapply all migrations: comments
Running migrations:
  Rendering model states... DONE
  Unapplying comments.0001_initial... OK
Operations to perform:
  Apply all migrations: admin, auth, comments, contenttypes, sessions
Running migrations:
  Applying comments.0001_initial... OK
Installed 10 object(s) from 1 fixture(s)
rinsaka@Macmini2020 django_comment %

目次に戻る