Django“get()得到了一个意想不到的关键字参数'pk'”错误 [英] Django "get() got an unexpected keyword argument 'pk'" error

查看:1484
本文介绍了Django“get()得到了一个意想不到的关键字参数'pk'”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的views.py的相应部分

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

我正在尝试重定向到我打算在创建一个对象的主页之后实现的页面。 p>

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 :)")

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

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'

有人请帮助我。

推荐答案

该函数获取的参数多于应用程序的一个参数。
将其更改为:

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

def get(self, request, pk):

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

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.

这篇关于Django“get()得到了一个意想不到的关键字参数'pk'”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆