【问题标题】:Receiving parameters with APIView使用 APIView 接收参数
【发布时间】:2017-11-06 12:18:14
【问题描述】:

我有这个路由:

   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()?

谢谢

【问题讨论】:

  • RSSList 不是问题,您在其他地方缺少*args, **kwargs。也许在ArticleList 而不是RSSList
  • 您在路由中使用ArticleList 类!但是你在 RSSList 类中获取 article_id 而不是!

标签: python django routing restful-url


【解决方案1】:

你也可以这样:

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):

【讨论】:

  • 以及如何注册这个可选参数?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 2012-04-05
  • 2012-09-26
  • 1970-01-01
相关资源
最近更新 更多