Search code examples
pythondjangoroutesrestful-url

Receiving parameters with APIView


I have this routing:

   url(r'^article/(?P<article_id>\d+)/', views.ArticleList.as_view())

which leads to this function:

class RSSList(APIView):
    def get(self, request, *args, **kwargs):
        article_id = kwargs.get('article_id')

But when I try to query something like /article/34

I get this error:

TypeError: get() got an unexpected keyword argument 'article_id'

How can I pass article_id to get()?

Thank you


Solution

  • You can get like this also:

    def get(self, request, article_id):
       print(article_id) #for >3.2
       print article_id # for 2.7
    

    If you want to make it optional:

    def get(self, request, article_id=None):