[英]Django CBV: Easy access to url parameters in get_context_data()?
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. 但是, self.kwargs
语法变得笨拙,尤其是在处理大量参数时。 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: 因此,在每个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.? 缺少覆盖get()
方法,子类化Django的通用视图等吗? I suspect I'm just missing some extremely basic, fundamental python concept here. 我怀疑我只是在这里缺少一些非常基本的,基本的python概念。
Here is the default get()
method that calls get_context_data()
in case it's helpful to reference here: 这是调用get_context_data()
的默认get()
方法, get_context_data()
在这里参考:
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). 我的错误是,调用get()
方法实际上如下(在这种情况下,通用FormView
被子类化了)。 Unfortunately the kwargs that are passed into get_context_data()
are not the same as self.kwargs
: 不幸的是,传递给get_context_data()
的self.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! 非常感谢!
If kwargs passed to get_context_data
is the same as self.kwargs
, You could do this: 如果传递给kwargs get_context_data
相同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. 编辑:您可以重写get方法来传递kwargs。我知道它并不优雅,但可以使用。 You could make a mixin to use in multiple classes. 您可以制作一个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.kwargs['...']
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.