使用API​​View接收参数

时间:2017-11-06 12:18:15

标签: python django routing restful-url

我有这个路由:

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

导致此功能:

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

但是当我尝试查询类似/ article / 34

的内容时

我收到此错误:

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

如何将article_id传递给get()?

谢谢

1 个答案:

答案 0 :(得分:0)

你也可以这样

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

如果使其成为选择,那么就像这样更新

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