基于Django类的视图:跨类的变量

时间:2013-10-22 13:16:00

标签: django django-models django-forms django-templates django-views

我在 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 文件中,我有以下ListView继承自上述GroupAwareMixin:

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

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

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

我尝试从ChatListView继承,但聊天类中的 self.group 为无。

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)

0 个答案:

没有答案