56

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.

Afzal S.H.
  • 1,084
  • 2
  • 14
  • 27

3 Answers3

108

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.

rohithpr
  • 6,050
  • 8
  • 36
  • 60
  • It is still passed in as a keyword argument, so will be present in **kwargs. Run it in a debugger, and it will be there. – wobbily_col May 14 '15 at 18:01
  • 1
    https://docs.djangoproject.com/en/1.8/topics/http/urls/#named-groups You are using the named argument in your URL regular expression (P), which as the docs say is passing it in as keyword arguments. And the error explicitly says "unexpected keyword argument". – wobbily_col May 14 '15 at 18:18
  • It keeps your code more future proof, if any more arguments get added to the url. Your answer was using a positional argument - which you have now changed in the comment. – wobbily_col May 14 '15 at 18:28
  • Sure that's correct, now that you have given it a default value, it will treat it as a keyword argument. The answer you posted isn't – wobbily_col May 14 '15 at 18:30
  • You are passing it in as a keyword argument and trying to retrieve it as a positional argument. http://stackoverflow.com/questions/1419046/python-normal-arguments-vs-keyword-arguments – wobbily_col May 14 '15 at 18:33
  • Ok, it does work your way after I checked. I would still saying using kwargs is more maintainable, especially if you are using class based generic views. You can pass **kwargs through to any other methods that may inherit from yours. – wobbily_col May 14 '15 at 18:51
  • Could you please check out http://stackoverflow.com/questions/30260201/django-typeerror-user-object-is-not-iterable I have got the answer to the original question but I'm having further troubles. Please go through the comment section of the accepted answer, the only one. – Afzal S.H. May 15 '15 at 16:18
22

add the kwargs into the method definition:

def get(self, request, *args, **kwargs):
    return HttpResponse("Created :)")
wobbily_col
  • 11,390
  • 12
  • 62
  • 86
  • Could you please check out http://stackoverflow.com/questions/30260201/django-typeerror-user-object-is-not-iterable I have got the answer to the original question but I'm having further troubles. Please go through the comment section of the accepted answer, the only one. – Afzal S.H. May 15 '15 at 16:18
-2

Check that if your views.fun_name is same as the function name in views

Shrey
  • 38
  • 3