class-based-view 参数
To access the url parameters in class based views, use self.args or self.kwargs
参数来源:
urlconf
未分组的传给args, 分组传给kwargs
例子
# 分组传给kwargs
url(r'^(?P<slug>[a-zA-Z0-9-]+)/$', MyView.as_view(), name = 'my_named_view')
在通用视图中使用参数:
class MyView(DetailView):
template_name = 'detail.html'
model = MyModel
def get_object(self, queryset=None):
print self.kwargs['slug'] # 参数使用
return queryset.get(slug=self.slug)
近期评论