T. *_*one 0 django django-views django-class-based-views
我搜索了SO和Django文档,似乎无法找到它.我正在扩展django.contrib.comments应用程序的基本功能,以使用我的webapp中的自定义权限系统.对于审核操作,我尝试使用基于类的视图来处理对注释和权限检查的基本查询. (在此上下文中的"EComment"是我的"增强评论",继承自基础django评论模型.)
我遇到的问题comment_id是从urls.py中的URL传入的kwarg.如何从基于类的视图中正确检索?
现在,Django正在抛出错误TypeError: ModRestore() takes exactly 1 argument (0 given).代码包括在下面.
urls.py
url(r'restore/(?P<comment_id>.+)/$', ModRestore(), name='ecomments_restore'),
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
ste*_*anw 10
您没有使用基于类的视图.你不小心写了def而不是class:
def ECommentModerationApiView(object):
...
def ModRestore(ECommentModerationApiView):
Run Code Online (Sandbox Code Playgroud)
应该是:
class ECommentModerationApiView(object):
...
class ModRestore(ECommentModerationApiView):
Run Code Online (Sandbox Code Playgroud)