如何调用超类字典并更新它 - Python

问题描述 投票:1回答:1

如何在Python中调用父类并加入两个字典

提前谢谢了。

class Base(View):
   title = 'abc'
   slug = 'abc-12'
   def get_context(self, request):
       return {'title': self.title, 'slug': self.slug }

class Logbook(Base)
   mode = 'read' 
   def get_context(self, request)
       # call super class 
       return super().add('mode': self.mode)
       # i want {'title': 'abc', 'slug': 'abc-12', mode: 'read' }

   def get(self, request)
       context = self.get_context(request)
django python-3.x
1个回答
0
投票

在python 3中,您可以通过以下方式调用父类的方法:

    def get_context(self, request)
       context = super().get_context(request)
       context.update({'mode': self.mode})
       return context
© www.soinside.com 2019 - 2024. All rights reserved.