我有这个 Django 模型:
#encoding:utf-8
from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
# Create your models here.
class Profile(models.Model):
user = models.ForeignKey(User, unique=True, related_name='user')
follows = models.ManyToManyField(User, related_name='follows', symmetrical=False, blank=True)
bio = models.TextField(blank=True)
avatar = models.ImageField(upload_to='avatar', blank=True)
website = models.CharField(max_length=100, blank=True)
place = models.CharField(max_length=100, blank=True)
def __unicode__(self):
return unicode(self.user)
admin.site.register(Profile)
admin.autodiscover()
然后,我有这样的看法:
def profile(request, user_id):
user = get_object_or_404(Profile, user=user_id)
return render_to_response('user_view.html', {'user':user,})
现在,在user_view.html
我显示“用户”数据(用户名、头像、网站等):
{{user.user_id}}
{{user.user}}
{{user.website}}
{{user.avatar.url}}
但是,我还需要显示我关注的用户的用户名、头像和网站。我这样做了:
{% for element in user.follows.all %}
<a href="{{element.website}}">{{element.user}}</a>
<img src="{{element.avatar.url}} />
{% endfor %}
但是,它不是显示用户的数据,而是显示:
<django.db.models.fields.related.RelatedManager object at 0x022F34F0>
但如果我这样做:
{% for element in user.follows.all %}
<p>{{element}}</p>
{% endfor %}
它完美地显示了用户的姓名,但我仍然无法显示网站或头像。我怎样才能做到这一点?