本文介绍了Django CBV:轻松访问get_context_data()中的url参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



但是, self.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,...
#而不是self.kwargs ['var1'],self.kwargs ['var2'],...

这是一个丑陋的痛苦,但它最终使事情变得更容易使用和阅读。



有一个简单的方法来清理它并将命名参数变成局部变量?没有覆盖 get()方法,将Django的通用视图进行子类化等等?我怀疑我只是在这里遗漏了一些非常基本的基本的python概念。



这是默认的 get()方法调用 get_context_data(),以便在此参考有用:

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

更新:



我的错误, get()方法实际上如下(在这种情况下,通用的 FormView 正在被子类化)。不幸的是,传递给 get_context_data()的kwarg与 self.kwargs 不同:

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

非常感谢!

解决方案

如果kwargs传递到 get_context_data self.kwargs 相同,您可以这样做:

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

编辑:你可以覆盖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))

除此之外,我真的不能想到一个更好的方法。您的原始解决方案可能最好。我通常直接访问 self.kwargs ['...']


I understand that the standard way of accessing named url parameters in a custom get_context_data() method is through 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:

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.

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

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:

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:

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.

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.

这篇关于Django CBV:轻松访问get_context_data()中的url参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:38