Django"get()得到了一个意外的关键字参数'pk'"错误

Afz*_*.H. 30 django

我试图重定向到一个页面,我打算在创建一个页面之后将其作为对象的主页实现.

下面是我的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 :)")
Run Code Online (Sandbox Code Playgroud)

和我的urls.py的相应部分;

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

但我得到了上述错误;

TypeError at /station/2
get() got an unexpected keyword argument 'pk'
Run Code Online (Sandbox Code Playgroud)

有人请帮帮我.

roh*_*hpr 62

该函数获得的一个参数超出了预期.将其更改为:

def get(self, request, pk):
Run Code Online (Sandbox Code Playgroud)

pk的值将等于已匹配的模式,并且由于您已指定它将是一个数字,因此pk的类型将为int.


wob*_*col 9

将kwargs添加到方法定义中:

def get(self, request, *args, **kwargs):
    return HttpResponse("Created :)")
Run Code Online (Sandbox Code Playgroud)