如何从不同的应用程序获取用户头像?

时间:2014-11-30 20:55:43

标签: python django

我有两个应用,评论&个人资料和我希望通过在他们离开的评论旁边获取用户个人资料图片来使他们一起工作。但是,当我尝试访问它时,它并没有出现。如何获取用户个人资料图片,而不仅仅是他们的名字?

profile.models

from django.contrib.auth.models import User
from django.db import models


class Profile(models.Model):
    user = models.OneToOneField(User, null=True, blank=True)
    picture = models.ImageField(null=True, blank=True)

    def __str__(self):
        return self.user.username

business.models

class BusinessReview(models.Model):
    business = models.ForeignKey(Business)
    user_id = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    description = models.TextField(max_length=350)
    recommend = models.BooleanField(default=True)
    rating = models.PositiveSmallIntegerField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return self.title

/templates/directory/single.html /

{% for review in business.businessreview_set.all %}

  <h4 id="review_title">{{ review.title }}<h4>
  <blockquote>{{ review.description }}</blockquote>
  <h5>{{ review.rating }}</h5>
  <p>{{ review.user }}</p> <!-- replace with profile.user.picture -->
  <br>
{% endfor %}

1 个答案:

答案 0 :(得分:0)

django将返回ImageField中的url,如下所示:

{% for review in business.businessreview_set.all %}

  <h4 id="review_title">{{ review.title }}<h4>
  <blockquote>{{ review.description }}</blockquote>
  <h5>{{ review.rating }}</h5>
  <img src="{{ review.user_id.profile.picture.url }}" />
  <br>
{% endfor %}