Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rest-framework "get() missing 1 required positional argument"

I want transmit a GET parameters 'pk' to django rest-framework.

Browser

enter image description here

but in my view.py I was setting my GET method to receive ‘pk’ parameters.

views.py

enter image description here

It's urls.py code:

enter image description here

and There is another one question,if I models object use 'objects' method in the pycharm,were throw an exception,such as:

enter image description here

but my friend was not happen this exception. He use pycharm 2017.4(macOS)

cateloydata = category.objects.all()

my pycharm version:pycharm 2017.2 python version 3.6 django version 1.11.7

Thank everyone.

and I'm sorry, I need at least 10 reputation to post images. So my question composing is very bad.

感谢!

like image 322
notyeat Avatar asked Sep 16 '25 04:09

notyeat


1 Answers

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

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(self, request): pk = request.GET.get('pk')

instead.

like image 126
arcegk Avatar answered Sep 19 '25 01:09

arcegk