其余框架& quot; get()缺少1个必需的位置参数" [英] rest-framework "get() missing 1 required positional argument"
问题描述
我想将GET参数'pk'传递给Django rest-framework.
I want transmit a GET parameters 'pk' to django rest-framework.
浏览器
但是在我的view.py中,我将GET方法设置为接收"pk"参数.
but in my view.py I was setting my GET method to receive ‘pk’ parameters.
views.py
这是urls.py代码:
It's urls.py code:
还有另一个问题,如果我在pycharm中使用'objects'方法对对象进行建模,则会抛出异常,例如:
and There is another one question,if I models object use 'objects' method in the pycharm,were throw an exception,such as:
但是我的朋友没有发生此异常.他使用pycharm 2017.4(macOS)
but my friend was not happen this exception. He use pycharm 2017.4(macOS)
cateloydata = category.objects.all()
我的pycharm版本:pycharm 2017.2python 3.6版django版本1.11.7
my pycharm version:pycharm 2017.2 python version 3.6 django version 1.11.7
谢谢大家.
很抱歉,发布图片需要至少10个声誉.因此,我的问题撰写非常糟糕.
and I'm sorry, I need at least 10 reputation to post images. So my question composing is very bad.
感谢!
推荐答案
在将参数添加到url时,必须在方法定义中添加一个额外的参数,如下所示:
When you are adding params to your url, you have to add an extra param to your method definition, like this:
# urls.py
url(r'^questions/(?P<pk>[\w:|-]+)/$', TheView.as_view(), name='view')
在上面的url中,您正在传递url参数(pk),因此必须在方法中接收它:
In the url above, you are passing a url param (pk) so you have to receive it in the method:
# views.py
class TheView(APIView):
def get(self, request, pk):
...
但是在您的情况下,您希望通过查询参数传递数据.
But in your case, you want to pass data by query params.
# urls.py
url(r'^questions/$', TheView.as_view(), name='view')
因此您不必在方法声明中接收它,请使用:
so you don't have to receive it in the method declaration, use:
def get(自身,请求):pk = request.GET.get('pk')
相反.
这篇关于其余框架& quot; get()缺少1个必需的位置参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!