Help us understand the problem. What is going on with this article?

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

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>
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした