私はブログアプリを持っており、外部キー値 "postype"に基づいて、クラスと表示/非表示部分を使用して非常に異なる方法で各投稿を表示したい。 これは私のコードです:Djangoがここで動作しない場合
{% for post in posts.object_list %}
<div class="{{ post.postype }}">
<h4>{{ post.title }}</h4>
{% if post.postype == 'Post' %}<p>{{ post.created }}</p>{% endif %}
</div>
{% endfor %}
そしてその結果は次のとおりです。
<div class="Post">
Title Post One
</div>
<div class="News">
Title Post Two
</div>
<div class="Post">
Title Post Three
</div>
だから私の質問は、なぜ「post.created」のdivクラスは「ポストを示していても表示されていませんさ"2つの場合?"は、ifが一致する必要があることを意味します。
これは私が
class Postype(models.Model):
postype = models.CharField(max_length=32)
def __unicode__(self):
return self.postype
class Post(models.Model):
author = models.ForeignKey(User)
postype = models.ForeignKey(Postype)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=100)
slug = models.SlugField()
text = models.TextField()
allow_comments = models.BooleanField(db_index=True, default=True)
published = models.BooleanField(db_index=True, default=True)
objects = PostManager()
def __unicode__(self):
return u"%s - %s" % (self.title, self.created)
def save(self, *args, **kwargs):
self.slug = slughifi(self.title)
super(Post, self).save(*args, **kwargs)
を使用しているモデルであるおかげ
どのモデルが使用されているか表示できますか? –