SOとDjangoのドキュメントを検索しましたが、これを見つけることができないようです。django.contrib.commentsアプリの基本機能を拡張して、Webアプリにあるカスタム権限システムを使用します。モデレートアクションでは、クラスベースのビューを使用して、コメントの基本的なクエリとそのコメントに対するアクセス許可のチェックを処理しようとしています。 (このコンテキストでの「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'),
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()