django频道无法使用文档中提到的django内置身份验证系统对用户进行身份验证

时间:2019-07-02 18:18:06

标签: python django django-channels django-login

我想运行一个需要在我的频道应用程序中通过身份验证的用户的使用者 断开与消费者的联系的渠道,称用户未通过身份验证

但用户已通过身份验证,并且其cookie在浏览器中已更新

我遵循了对用户进行身份验证的渠道文档

class LikeConsumer(AsyncJsonWebsocketConsumer):

    async def connect(self):

        self.user = self.scope["user"]
        # user = self.scope['user']
        user=self.user
        print(user)

        if user.is_anonymous:
            await self.close()
            print("user is anonymous")
        else:
            await self.accept()
            # user_group = await self._get_user_group(self.scope['user'])

            await self.channel_layer.group_add("{}".format(user.id), self.channel_name)
            print(f"Add {self.channel_name} channel to post's group")
            print('connected')
    # @database_sync_to_async
    # def _get_user_group(self, user):
    #   if not user.is_authenticated:
    #       raise Exception('User is not authenticated.')
    #   else:
    #       print("user is not authenticated")
    #   return user

    async def disconnect(self,close_code):
        user = self.scope['user']

        await self.channel_layer.group_discard("{}".format(user.id), self.channel_name)
        print(f"Remove {self.channel_name} channel from post's group")

我不确定错误到底是什么

user is anonymous
    WebSocket DISCONNECT /like/ [127.0.0.1:50710]

0 个答案:

没有答案