Kwargs和Django中基于类的意见
-
24-09-2019 - |
题
我如此搜查和Django的文档和不能似乎能够找到这一点。我延长了的 django.contrib.comments 的应用程序的基本功能使用自定义权限系统,它在我的web应用程序。对于适度的行动,我正在尝试使用基于类的观点来处理就可以了评论和权限检查的基本查询。 ( “EComment” 在此上下文中是我的 “增强评论”,从所述基部的django Comment模型继承。)
时,comment_id
在传递一个kwarg从URL中urls.py.时遇到的问题如何从一个基于类的视图检索此正确?
现在,Django是引发错误TypeError: ModRestore() takes exactly 1 argument (0 given)
。代码包含下面。
<强> urls.py 强>
url(r'restore/(?P<comment_id>.+)/$', ModRestore(), name='ecomments_restore'),
<强> views.py 强>
def ECommentModerationApiView(object):
def comment_action(self, request, comment):
"""
Called when the comment is present and the user is allowed to moderate.
"""
raise NotImplementedError
def __call__(self, request, comment_id):
c = get_object_or_404(EComment, id=comment_id)
if c.can_moderate(request.user):
comment_action(request, c)
return HttpResponse()
else:
raise PermissionDenied
def ModRestore(ECommentModerationApiView):
def comment_action(self, request, comment):
comment.is_removed = False
comment.save()
解决方案
您使用的不是基于类的视图。你不小心写的,而不是def
class
:
def ECommentModerationApiView(object):
...
def ModRestore(ECommentModerationApiView):
也许应该是:
class ECommentModerationApiView(object):
...
class ModRestore(ECommentModerationApiView):
其他提示
另外,你的URL模式需要的样子:
url(r'restore/(?P<comment_id>.+)/$', ModRestore.as_view(), name='ecomments_restore'),
不隶属于 StackOverflow