基于Django类的通用视图URL变量传递

时间:2021-03-04 19:16:39

Question is quite simple.

问题很简单。

Let's say I have an URL config with line:

假设我有一个带有行的URL配置:

url(r'^models/(?P<model_group_id>[0-9]+)/(?P<page>\d+)/$', 'Group'),

And I want to access model_group_id variable inside

我想在里面访问model_group_id变量

class Group(ListView)

View.

视图。

On simple views I would just change Group description to:

对于简单的视图,我只需将组描述更改为:

class Group(ListView, model_group_id):

And it would work. Now it says that model_group_id is not defined. So how to pass variables from url regex to class-based view?

它是可行的。现在它说model_group_id没有定义。那么如何将变量从url regex传递到基于类的视图呢?

1 个解决方案

#1


22  

You can access positional arguments in self.args and name-based arguments in self.kwargs.

您可以在self中访问位置参数。在self。kwargs中基于args和名称的参数。

class Group(ListView): 

    def get_queryset(self):
        model_group_id=self.kwargs['model_group_id']
        ...

See the docs for more info.

更多信息请参阅文档。

#1


22  

You can access positional arguments in self.args and name-based arguments in self.kwargs.

您可以在self中访问位置参数。在self。kwargs中基于args和名称的参数。

class Group(ListView): 

    def get_queryset(self):
        model_group_id=self.kwargs['model_group_id']
        ...

See the docs for more info.

更多信息请参阅文档。