modles.pyでarticle_tableモデルデータを定義し、特に
options
を設定します
status
の属性
class Article(models.Model):
STATUS = (
(0, 'normal'),
(-1, 'deleted'),
)
block = models.ForeignKey(Block, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
content = models.CharField(max_length=1000) # set the widget
status = models.IntegerField(choices=STATUS)
date_created = models.DateTimeField(default=datetime.now)
date_updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
応答したhtmlを確認すると、
0
でステータスが表示されます「通常」という説明ではなく、データベースから
ステータスに
normal
が表示されませんでした意図したとおり。
article_list.html
のテンプレート
<table class="table table-bordered">
<tr>
<th>Status</th>
<th>Title</th>
<th>Author</th>
<th>Latest Updated</th>
</tr>
{% for article in articles %}
<tr>
<td>{{ article.status }}</td>
<td>{{ article.title }}</td>
<td>{{ article.author }}</td>
<td>{{ article.date_updated }}</td>
</tr>
{% endfor %}
</table>
このような問題を解決する方法は?
choices
を使用する場合 、それからget_<fieldname>_display()
を使用できます 次のようなテンプレートで: