Django CBV:容易访问get_context_data()中的url参数?

时间:2023-01-26 18:20:15

I understand that the standard way of accessing named url parameters in a custom get_context_data() method is through self.kwargs.

我理解在自定义get_context_data()方法中访问命名url参数的标准方法是通过self.kwargs。

However, the self.kwargs syntax becomes awkward, especially when handling a significant number of parameters. So, I've been resorting to something like this at the top of every get_context_data() method -- just to get easy-to-handle local variables:

然而,自我。kwargs语法变得很笨拙,尤其是在处理大量参数时。因此,我一直在每个get_context_data()方法的顶部使用类似的方法——只是为了获得易于处理的本地变量:

def get_context_data(self, **kwargs):
    var1, var2, var3, var4, var5 = [self.kwargs[x] for x in ['var1', 'var2', 'var3', 'var4', 'var5']]
    # do stuff with var1, var2, ...
    # instead of self.kwargs['var1'], self.kwargs['var2'], ...

This is ugly and a pain, but it ultimately makes things a lot easier to work with and read.

这很难看,也很痛苦,但最终会让事情更容易处理和阅读。

Is there a simple way to clean it up and get named parameters into local variables? Short of overriding the get() method, subclassing Django's generic views, etc.? I suspect I'm just missing some extremely basic, fundamental python concept here.

是否有一种简单的方法来清理它并将命名参数转换为局部变量?除了覆盖get()方法、子类化Django的泛型视图等等之外。我怀疑我只是错过了一些非常基本的python概念。

Here is the default get() method that calls get_context_data() in case it's helpful to reference here:

这里是调用get_context_data()的默认get()方法,如果在这里引用会有帮助的话:

def get(self, request, *args, **kwargs):
    context = self.get_context_data(**kwargs)
    return self.render_to_response(context)

UPDATE:

更新:

My mistake, the calling get() method is actually as follows (The generic FormView is being subclassed in this case). Unfortunately the kwargs that are passed into get_context_data() are not the same as self.kwargs:

我的错误是,调用get()方法实际上如下所示(在本例中,通用的FormView被子类化)。不幸的是,传递到get_context_data()的kwargs与self.kwargs不一样:

def get(self, request, *args, **kwargs):
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    return self.render_to_response(self.get_context_data(form=form))

Many thanks!

很多谢谢!

1 个解决方案

#1


1  

If kwargs passed to get_context_data is the same as self.kwargs, You could do this:

如果传递给get_context_data的kwargs与self相同。kwargs,你可以这么做

def get_context_data(self, var1, var2, var3, var4, var5, **kwargs):
    # do stuff with var1, var2, ...

EDIT: you could override the get method to pass the kwargs in. I know its not elegant, but it would work. You could make a mixin to use in multiple classes.

编辑:您可以重写get方法来传入kwargs。我知道它不优雅,但它会起作用。您可以在多个类中使用mixin。

def get(self, request, *args, **kwargs):
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    return self.render_to_response(self.get_context_data(form=form, **kwargs))

Other than that, I really can't think of a better way. Your original solution might be best. I usually just access self.kwargs['...'] directly.

除此之外,我实在想不出更好的办法了。你最初的解决方案可能是最好的。我通常只访问self。直接']。

#1


1  

If kwargs passed to get_context_data is the same as self.kwargs, You could do this:

如果传递给get_context_data的kwargs与self相同。kwargs,你可以这么做

def get_context_data(self, var1, var2, var3, var4, var5, **kwargs):
    # do stuff with var1, var2, ...

EDIT: you could override the get method to pass the kwargs in. I know its not elegant, but it would work. You could make a mixin to use in multiple classes.

编辑:您可以重写get方法来传入kwargs。我知道它不优雅,但它会起作用。您可以在多个类中使用mixin。

def get(self, request, *args, **kwargs):
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    return self.render_to_response(self.get_context_data(form=form, **kwargs))

Other than that, I really can't think of a better way. Your original solution might be best. I usually just access self.kwargs['...'] directly.

除此之外,我实在想不出更好的办法了。你最初的解决方案可能是最好的。我通常只访问self。直接']。