0

我搜索了 SO 和 Django 文档,但似乎无法找到它。我正在扩展django.contrib.comments应用程序的基本功能,以使用我的 web 应用程序中的自定义权限系统。对于审核操作,我尝试使用基于类的视图来处理评论的基本查询和对其的权限检查。 (在这种情况下,“EComment”是我的“增强评论”,继承自基本的 django 评论模型。)

我遇到的问题comment_id是从 urls.py 中的 URL 传入的 kwarg。如何从基于类的视图中正确检索它?

现在,Django 正在抛出错误TypeError: ModRestore() takes exactly 1 argument (0 given)。代码包含在下面。

网址.py

url(r'restore/(?P<comment_id>.+)/$', ModRestore(), name='ecomments_restore'),

视图.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()
4

2 回答 2

10

您没有使用基于类的视图。你不小心写了def而不是class

def ECommentModerationApiView(object):
...
def ModRestore(ECommentModerationApiView):

应该是:

class ECommentModerationApiView(object):
...
class ModRestore(ECommentModerationApiView):
于 2010-03-04T01:00:59.527 回答
3

此外,您的 url 模式需要如下所示:

url(r'restore/(?P<comment_id>.+)/$', ModRestore.as_view(), name='ecomments_restore'),
于 2011-04-20T14:29:57.980 回答