我想在模板中显示我的粉丝的姓名和个人资料照片。我只能显示粉丝的名字。请提示

时间:2016-07-06 13:31:29

标签: python django django-templates django-views

这是我的UserProfile models.py文件

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

# Create your models here.
class UserProfile(models.Model):
    GENDER_CHOICES=(
        ('M','Male'),
        ('F','Female'),
    )
    user=models.OneToOneField(User,null=True,blank=True)
    name=models.CharField(max_length=100)
    gender=models.CharField(max_length=1,choices=GENDER_CHOICES)
    age=models.IntegerField(null=True)
    pic=models.ImageField(null=True,blank=True)
    follow_tags=models.ManyToManyField("question.Tag"   )
    bio=models.TextField(blank=True)
    mobile=models.CharField(max_length=12,unique=True,default='')
    college=models.CharField(max_length=1024,default="not specified ")
    address=models.TextField(default="not specified")
    followers=models.ManyToManyField('auth.User',related_name='followers',blank=True)
    following=models.ManyToManyField('auth.User',related_name='following',blank=True)
    #profile_pic=models.ImageField(upload_to='profile_pics/',blank=True)
    User.profile=property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

以下是添加关注者的功能

def follow(request, userprofile_id=1):
    x = UserProfile.objects.get(id=userprofile_id)
    p=request.user
    t=x.user.id
    x.followers.add(p)
    x.save()
    return HttpResponseRedirect('/account/profile/get/%s' %t)

这是我向关注者展示的观点

def show_followers(request,userprofile_id=1):
    return render(request,'show_followers.html',
              {'userprofile':UserProfile.objects.get(id=userprofile_id)}
             )

最后这是show_followers模板

{% extends "base.html" %}
{% load bootstrap_toolkit %}
{% block content %}
<div class="row">
<div class="col-md-3">
{% if userprofile.pic %}
<p><img src="{{userprofile.pic.url}}" width="150" height="150" class="img-circle"/></p>
{% endif %}
</div>
<div class="col-md-9">
<h1>{{userprofile.name}}</h1>
</div>
</div>
<div class="row">
<div class="col-md-3">
<h5>Answers</h5>
<h5>Questions</h5>
<h5>Followers | {{userprofile.followers.count}}</h5>
<h5>Following | </h5>
</div>
<p>{{userprofile.followers.count}} Followers</p>
<h4>{% for c in userprofile.followers.all %}
<div class="col-md-4">
<a href="/account/profile/get/{{c.id}}">{{c}}</a>
</div>
{% endfor %}
</div>
{% endblock %}

在userprofile.followers.all中循环只能显示关注者的名字..我必须显示与关注者链接的每个细节。提前谢谢

1 个答案:

答案 0 :(得分:0)

鉴于用户,您可以通过userprofile访问者访问UserProfile。

 {{ c.userprofile.age }}