0

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()
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 に答える