Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django "get() got an unexpected keyword argument 'pk'" error

Tags:

django

I am trying to redirect to a page I intend to implement as an object's homepage after creation of one.

Below is corresponding part of my views.py

            new_station_object.save()
            return HttpResponseRedirect(reverse("home_station", 
                                                kwargs={'pk':   new_station_object.id}
            ))

class StationHome(View):
    def get(self, request):
        return HttpResponse("Created :)")

and corresponding part of my urls.py;

    url(r'^station/(?P<pk>\d+)$', StationHome.as_view(),    name='home_station'),

But I get the said error;

TypeError at /station/2
get() got an unexpected keyword argument 'pk'

Someone please help me out.

like image 911
Afzal S.H. Avatar asked May 14 '15 17:05

Afzal S.H.


2 Answers

The function is getting one argument more than it is supposed to. Change it to:

def get(self, request, pk):

The value of pk will be equal to the pattern that has been matched, and since you've specified that it's going to be a number, the type of pk will be int.

like image 155
rohithpr Avatar answered Nov 15 '22 05:11

rohithpr


add the kwargs into the method definition:

def get(self, request, *args, **kwargs):
    return HttpResponse("Created :)")
like image 19
wobbily_col Avatar answered Nov 15 '22 05:11

wobbily_col