[英]How do i pass parameters to a class based view in django?
I have the following class based view; 我有以下基于类的视图;
class myClassView():
def get(self):
# lots of code ...
return response
My urlconf for this looks like 我的urlconf看起来像
(r^'call_myClassView/', myClassView.as_view())
I want to pass parameters to the urlconf the old functional way 我想以旧的功能方式将参数传递给urlconf
(r'call_myClassView/(?P<id>\w+)/$',myClassView.as_view())
How do i pass parameters to my urlconf and how do i receive the parameter in my class view. 如何将参数传递给我的urlconf以及如何在类视图中接收参数。
They are passed in the old way. 他们在旧的方式通过。
You access them via self.args
and self.kwargs
, for positional and keyword arguments respectively. 您可以通过self.args
和self.kwargs
分别访问位置和关键字参数。 In your case, self.kwargs['id']
would do the trick. 在你的情况下, self.kwargs['id']
可以解决问题。
Edit because you've overridden get()
but not preserved the signature. 编辑,因为您已覆盖get()
但未保留签名。 If you're overriding a method, always do def get(self, request, *args, **kwargs)
. 如果你要覆盖一个方法,总是做def get(self, request, *args, **kwargs)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.