Receiving parameters with APIView
Asked Answered
B

1

5

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

Brisance answered 6/11, 2017 at 12:18 Comment(2)
RSSList is not the problem, you are missing *args, **kwargs somewhere else. Maybe in ArticleList instead of RSSList?Haywire
You're using ArticleList class in your routing! But you're fetching article_id inside of RSSList class instead!Sauterne
E
10

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):
Elecampane answered 6/11, 2017 at 12:21 Comment(1)
And how to register this optional or not parameter?Frier

© 2022 - 2024 — McMap. All rights reserved.