我最近在我的 Django 应用程序中切换到基于类的视图,并希望尽可能优雅地使用它们。在应用程序中,我有一个评论系统,如果权限匹配,管理员应该能够删除和/或发布/取消发布评论。我为此写了一个RedirectView
,包括两个功能:
class CommentChangeView(RedirectView, SingleObjectMixin):
"""
A redirection that acts on a Comment. The url parameter
"action" is taken as a class function and executed.
"""
model = Comment
def get_redirect_url(self, pk):
"""
Redirect to the article page, always.
"""
return reverse('post', args=(self.object.post.slug,))
def get(self, *args, **kwargs):
"""
Here, it is decided what to execute.
"""
self.object = self.get_object()
func = getattr(self, kwargs.pop('action', None), None)
if callable(func):
func()
return super(CommentChangeView, self).get(*args, **kwargs)
@method_decorator(permission_required('blog.delete_comment'))
def delete(self):
"""
Delete the comment
"""
self.object.delete()
messages.success(self.request, 'Comment deleted.')
@method_decorator(permission_required('blog.change_comment'))
def toggle_publish(self):
"""
Toggle its publication state
"""
self.object.published = not self.object.published
self.object.save()
messages.success(self.request, 'Comment toggled.')
现在的问题是,我希望这两个操作具有不同的权限 - 因此有不同的装饰器。通常,dispatch
功能是装饰的。上面的代码不起作用,我得到一个TypeError
. 没有装饰器,它可以完美地工作。
我将如何实施这种情况?或者我应该将删除和发布的视图分开吗?
这是 StackTrace 的TypeError
:
Internal Server Error: /comment/toggle_publish/1/
Traceback (most recent call last):
File "/***/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/***/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/***/lib/python2.7/site-packages/django/views/generic/base.py", line 86, in dispatch
return handler(request, *args, **kwargs)
File "/***/blog/blog/views.py", line 186, in get
func()
File "/***/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in _wrapper
return bound_func(*args, **kwargs)
TypeError: _wrapped_view() takes at least 1 argument (0 given)