class BaseDeleteViewMixin():
template_name = 'confirm_delete.html'
def get_success_url(self):
return reverse_lazy('index-' + self.model._meta.model_name)
def post_override(self, view_class, request, *args, **kwargs):
self.object = self.get_object()
if "cancel" in request.POST:
url = self.get_success_url()
return HttpResponseRedirect(url)
else:
return super(view_class, self).post(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
klass = type(self)
return self.post_override(klass, request, *args, **kwargs)
class PersonDeleteView(BaseDeleteViewMixin, DeleteView):
model = Person
我收到以下错误:
Request Method: POST
Request URL: http://192.168.33.10:8000/contractor/person/111/delete/
Django Version: 1.7.4
Exception Type: RuntimeError
Exception Value: maximum recursion depth exceeded while calling a Python object
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py in __call__, line 59
方法帖子,放在PersonDeleteView
中 class PersonDeleteView(BaseDeleteViewMixin, DeleteView):
model = Person
def post(self, request, *args, **kwargs):
klass = type(self)
return self.post_override(klass, request, *args, **kwargs)
工作正常。 如何正确覆盖BaseDeleteViewMixin中的帖子?
我的目标是创建任何ModelNameDeleteView而不覆盖其中的帖子。但...
我向下一个解决方案倾斜:
class BaseDeleteViewMixin():
# other code
def post_override(self, request, *args, **kwargs):
self.object = self.get_object()
if "cancel" in request.POST:
url = self.get_success_url()
return HttpResponseRedirect(url)
else:
return super(kwargs["view_class"], self).post(request)
class PersonDeleteView(BaseDeleteViewMixin, DeleteView):
model = Person
def post(self, request, *args, **kwargs):
return self.post_override(request, view_class=PersonDeleteView)
答案 0 :(得分:0)
type(self)
将返回您的对象实际类(PersonDeleteView
),而不是该代码所在的类(BaseDeleteViewMixin
),因此super
将在{的上下文中启动{1}}。这将导致从PersonDeleteView
再次发布post方法,而不是BaseDeleteViewMixin
(正如您所期待的那样)。
只需在DeleteView
来电中对您的mixin类名进行硬编码。