加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

django – ‘dict’对象没有属性朋友

发布时间:2020-12-20 11:22:12 所属栏目:Python 来源:网络整理
导读:我收到此错误:渲染时捕获AttributeError:’dict’对象没有属性’friendship’.问题是当我尝试在自定义模板标记中获取friend.friend的值时.模块’friends_for_user’是对的.我有: 楷模 class FriendshipManager(models.Manager): def friends_for_user(sel
我收到此错误:渲染时捕获AttributeError:’dict’对象没有属性’friendship’.问题是当我尝试在自定义模板标记中获取friend.friend的值时.模块’friends_for_user’是对的.我有:

楷模

class FriendshipManager(models.Manager):

  def friends_for_user(self,user):
     friends = []
     for friendship in self.filter(from_user=user).select_related(depth=1):
        friends.append({"friend": friendship.to_user,"friendship": friendship})
     for friendship in self.filter(to_user=user).select_related(depth=1):
        friends.append({"friend": friendship.from_user,"friendship": friendship})
     return friends

模板标签

def render(self,context):
    user = self.user.resolve(context)
    num = self.num.resolve(context)

    my_friends = Friendship.objects.friends_for_user(user)

    potential_friends = []
    for friend in my_friends:
        potential_friends.append(Friendship.objects.friends_for_user(friend.friend))  //This line is the error.

    context[self.context_name] = potential_friends
    return ''

解决方法

看起来你正在使用字典而不是对象. 尝试 ?potential_friends.append(Friendship.objects.friends_for_user(朋友[ ‘朋友’]))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读