我有一个博客应用程序,我想以一种非常不同的方式显示每个帖子,使用类和显示/不显示部分,基于外键值“postype”。 这是我的代码:Django if tag does not work here
{% 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类显示“后“在两种情况下,这意味着如果应该匹配。
这是模型我使用
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)
感谢
你可以显示什么模型正在使用? –