1

我在mixins.py文件中有一个名为GroupAwareMixin的 mixin :

class GroupAwareMixin(object):
    group = None

    def get_group(self):
        self.group = self.bridge.get_group()

    def dispatch(self, request, *args, **kwargs):
        if not self.group:
            self.get_group()

views.py文件中,我有以下继承自上述GroupAwareMixin 的ListView:

class ChatListView(LoginRequiredMixin, GroupAwareMixin, ListView):
   model = Chat
   template_name = 'chat/home.html'

此外,我的 views.py 文件中有一个Chat类,我想从上面的 GroupAwareMixin 类中访问group变量。我如何能够访问 Chat 类中的组变量?

class Chat(ws.WS, ChatListView):
    def on_message(self, websocket, msg):
        slug = self.group

我尝试从ChatListView继承,但是Chat类中的self.group是None。

LoginRequiredMixin定义了以下内容

class LoginRequiredMixin(AccessMixin):
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated():
            if self.raise_exception:
                raise PermissionDenied  # return a forbidden response
            else:
                return redirect_to_login(request.get_full_path(),
                                     self.get_login_url(),
                                     self.get_redirect_field_name())

        return super(LoginRequiredMixin, self).dispatch(
            request, *args, **kwargs)
4

0 回答 0