Skip to content

Can't decorate a method with @api_view #9427

Discussion options

You must be logged in to vote

Correct, this decorator only works with function, not class methods. If you really insist, you may try your luck with Django's method_decorator: https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.decorators.method_decorator

Something along the lines of:

class Foo:
    @method_decorator(api_view())
    def index(self, request):
            return Response("Hello world")
    def get_urls(self):
        return [path(''), self.index, name='index], "rest_admin", "rest_admin"

I have not tested it with api_view, but this worked for me in other similar cases.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ZeyadMoustafaKamal
Comment options

Answer selected by ZeyadMoustafaKamal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants