私は次のように私は、3クラスを作成したい、のpythonの初心者です:Python、どのようにクラスを作成するバッチですか?
class ProtectTemplate(TemplateView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ProtectTemplate, self).dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context['phone'] = xxx
return context
class ProtectList(ListView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ProtectList, self).dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context['phone'] = xxx
return context
class ProtectDetail(DetailView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ProtectDetail, self).dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context['phone'] = xxx
return context
私はそれは恐ろしいことだと思います。だから私は次のようにしようとします:
login_class = [
('ProtectTemplate', TemplateView),
('ProtectList', ListView),
('ProtectDetail', DetailView),
]
for c, v in login_class:
class c(v):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(self.__class__, self).dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context['phone'] = xxx
return context
しかし、それは動作しません。とにかく3クラスを作成するバッチはありますか?
第二の試みは動作しませんどのように**を教えてください。 'self(self .__ class__、self)' **は、 'self .__ class__'がサブクラス化されたときに正しいクラスではないので、無限の再帰エラーにつながることに注意してください。 –
David Beazleyの[this](https://www.youtube.com/watch?v=sPiWg5jSoZI)トークに興味があるかもしれません。 –