我不知道为什么我收到这个错误..
['用户'对象不可迭代]
在这里,我想在主页中打印(登录用户)粉丝.除了错误是我的models.py是对的?追随者和追随机制.
模型
class Follow(models.Model):
following = models.ForeignKey('auth.User', related_name='following')
followers = models.ForeignKey('auth.User', related_name='followers')
Run Code Online (Sandbox Code Playgroud)
视图
def profile(request):
current_user = request.user
twi = Follow.objects.get(pk=current_user.id)
display = twi.followers
return render(request,'home.html' ,
{'detail':display,'user':current_user,})
Run Code Online (Sandbox Code Playgroud)
模板
{% for o in detail %}
<h1>o.followers</h1>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
你的逻辑中有一个混合,你的detail引用,followers但字段本身是一个单一用户的链接,你需要使这个字段成为ManyToMany关系,或者使用反向查找来查找用户遵循的内容.
(在你的上下文中也有一个流浪逗号,这可能会在以后引起问题.