0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Djangoで区分値を定義し、簡単に画面へ反映する

Last updated at Posted at 2020-01-24

Djangoで区分値を管理する

要件

model.pyで区分値を定義し、履歴のリストを表示するhtmlページへ反映します。

1. モデルに定義する

HistoryテーブルにSTATUSという区分値を持たせます。
ポイント:choices=

manage.py
class History(models.Model):
    STATUS = (
        ('001', '実施前'),  # BEFORE_EXECUTE
        ('011', 'ログイン中'),  # DURING_LOGIN
        ('012', '取得中'),  # WHILE_GETTING
        ('101', '完了'),  # DONE
        ('401', '失敗')  # fail
    )
    
    start_at = models.DateTimeField()
    status = models.CharField(max_length=3, choices=STATUS)

2. URLを定義する

履歴のリストを表示するページへのURLを定義。

url.py
urlpatterns = [
    # リスト
    path('history_list', views.HistoryListView.as_view(), name='history_list'),
]

3. ListViewに定義する

履歴のリストを表示するページのhtmlへ渡すViewを定義。

class HistoryListView(ListView):
    model = History
    template_name = 'history_list.html'

4. Htmlへ反映させる

ポイント:get_FIELD_diaplay

example.html
<td>
    {{ history.get_status_display }}
</td>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?