如何解决“无法解压不可迭代的Profile对象”错误?

时间:2019-06-15 00:05:48

标签: python django

我当时正在使用django为我的大学分配一个项目。我想使用Django创建一个跟随器系统,我编写了代码,并遇到以下错误

  

无法解包不可迭代的Profile对象

models.py

class Profile(models.Model):
    user=models.OneToOneField(User, on_delete=models.CASCADE)
    image=models.ImageField(default='default.jpg',upload_to='profile_pics',blank=True)
    description=models.TextField(max_length=200, blank=True)
    following = models.ManyToManyField(User, related_name='followed_by', blank=True)
    def __str__(self):
        return f'{self.user.username} Profile'

    def saveimg(self):
        super().save()

        img=Image.open(self.image.path)

        if img.height>300 or img.width>300:
            output_size=(300,300)
            img.thumbnail(output_size)
            img.saveimg(self.image.path)

views.py

def follow_user(request, username):
    creator = get_object_or_404(Profile, user__username__icontains=username)
    is_followed=False
    if request.user.profile.following.filter(creator).exists():
        request.user.profile.following.remove(creator)
        is_followed=False
    else:
        request.user.profile.add(following=creator)
        is_liked=True
    context={
        'creator':creator,
        'is_followed': request.user.profile.filter(following=creator).exists(),
        }
    return HttpResponseRedirect(home)

urls.py

path('user/<str:username>/follow', views.follow_user,name='follow_user'),

追踪

跟踪:

  

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ exception.py“   在内部     34. response = get_response(request)

     

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ base.py“   在_get_response中     126. response = self.process_exception_by_middleware(e,request)

     

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ base.py“   在_get_response中     124. response = wrapd_callback(request,* callback_args,** callback_kwargs)

     

follow_user中的文件“ D:\ sb \ blog \ views.py”     186. if request.user.profile.following.filter(creator).exists():

     

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ db \ models \ manager.py“   在manager_method中     82. return getattr(self.get_queryset(),name)(* args,** kwargs)

     

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ db \ models \ query.py“   在过滤器中     844. return self._filter_or_exclude(False,* args,** kwargs)

     

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ db \ models \ query.py“   在_filter_or_exclude中     862. clone.query.add_q(Q(* args,** kwargs))

     

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ db \ models \ sql \ query.py“   在add_q中     1263.子句,_ = self._add_q(q_object,self.used_aliases)

     

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ db \ models \ sql \ query.py“   在_add_q中     1287. split_subq = split_subq,

     

文件“ C:\ Users \ Mustafa   Lakhani \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ db \ models \ sql \ query.py“   在build_filter中     1161. arg,值= filter_expr

     

异常类型:/ user / mustafalakhani /关注异常的TypeError   值:无法解包不可迭代的Profile对象

0 个答案:

没有答案