【发布时间】:2015-02-09 04:22:09
【问题描述】:
所以,我正在尝试用 Django 写博客,但现在我遇到了 cmets 问题。 在每个帖子下面我都有用户 cmets ,带有评论作者姓名、图片和评论。
评论类:
class Comments(models.Model):
body = models.TextField()
author = models.ForeignKey(User, related_name= 'upser')
image = models.OneToOneField(UserProfile , related_name='im')
timestamp = models.DateTimeField(auto_now=True)
post = models.ForeignKey(Post, related_name='comments')
class Meta:
ordering = ('-timestamp',)
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='oser')
picture = models.ImageField(upload_to='profile_images', blank=True)
def __unicode__(self):
return self.user
所以,我有与 UserProfile 类连接的 cmets 类,其中我有用户的图像。
这就是我在模板中显示所有 cmets 的内容:
{% for comment in comments %}
<li>
<div class="media" style="margin-top: 10px">
<a class="pull-left" href="#">
<img class="media-object" src={{ MEDIA_ROOT }}/{{ PLACE FOR USER'S PICTURE }} alt="" width="80px" height="80px" >
</a>
<div class="media-body">
<h4 class="media-heading">{{ comment.author}}
<small>{{ comment.timestamp }}</small>
</h4>
{{ comment.body }}
</div>
</div>
</li>
{% endfor %}
还有一个问题,如果我有 Comments 对象,我如何从模板访问用户图像?
【问题讨论】:
标签: python django django-templates