将url参数传递给Django向导。

时间:2022-03-02 15:16:39

As the title implies, is there a way to pass url parameters to a Django wizard?

如标题所示,是否有方法将url参数传递给Django向导?

For example, in a normal view you can use the *some_parameter* value of the urlpattern:

例如,在普通视图中,您可以使用urlpattern的*some_parameter*值:

r'^blah/(?P<some_parameter>\d{8})/$

Is it possible to do the same when there is a wizard at the specified address? Perhaps add it to the initial data dictionary for each form?

在指定的地址中有向导时,是否可以执行相同的操作?也许将它添加到每个表单的初始数据字典中?

2 个解决方案

#1


1  

If this view is based on regular Django class-based views it should take any URL arguments you pass to it, and make it accessible to its methods under self.args and self.kwargs.

如果这个视图基于常规的基于Django类的视图,那么它应该接受您传递给它的任何URL参数,并使它在self下可以被它的方法访问。参数和self.kwargs。

See example for ListView in Django docs. In your case you'd see the argument in self.kwargs['some_parameter'].

参见Django文档中的ListView示例。在您的例子中,您将看到self.kwargs['some_parameter']中的参数。

#2


0  

Many of the generic class based views offer this functionality probably the most extensible one to capture any value that you could use would be TemplateView(). Sub-class TemplateView() and it will give you access to a context object name called params. It will be populated with variables from the url that invokes TemplateView() which it parses any variable parameters you give it.

许多基于类的通用视图都提供了这个功能,其中最可扩展的是TemplateView(),它可以捕获您可以使用的任何值。子类TemplateView(),它将让您访问一个名为params的上下文对象名称。它将使用调用TemplateView()的url中的变量进行填充,并解析您提供的任何变量参数。

The above answer is the most correct answer for a single model based list.

上面的答案是基于单一模型的列表的最正确答案。

#1


1  

If this view is based on regular Django class-based views it should take any URL arguments you pass to it, and make it accessible to its methods under self.args and self.kwargs.

如果这个视图基于常规的基于Django类的视图,那么它应该接受您传递给它的任何URL参数,并使它在self下可以被它的方法访问。参数和self.kwargs。

See example for ListView in Django docs. In your case you'd see the argument in self.kwargs['some_parameter'].

参见Django文档中的ListView示例。在您的例子中,您将看到self.kwargs['some_parameter']中的参数。

#2


0  

Many of the generic class based views offer this functionality probably the most extensible one to capture any value that you could use would be TemplateView(). Sub-class TemplateView() and it will give you access to a context object name called params. It will be populated with variables from the url that invokes TemplateView() which it parses any variable parameters you give it.

许多基于类的通用视图都提供了这个功能,其中最可扩展的是TemplateView(),它可以捕获您可以使用的任何值。子类TemplateView(),它将让您访问一个名为params的上下文对象名称。它将使用调用TemplateView()的url中的变量进行填充,并解析您提供的任何变量参数。

The above answer is the most correct answer for a single model based list.

上面的答案是基于单一模型的列表的最正确答案。