[英]Django CBV : get() and get_context_data()
I would like to get some indicators about get()
and get_context_data()
classes because I get an issue and I'm trying to understand why. 我想获取有关get()
和get_context_data()
类的一些指标,因为我遇到了问题,并且试图理解原因。
I have a Django DetailView
which let to display some statistics with multiple querysets. 我有一个Django DetailView
,可以显示带有多个查询集的一些统计信息。 In the same class, I have a query string
which shows the result from a get queryset. 在同一个类中,我有一个query string
,其中显示了来自get queryset的结果。
My code looks like this : 我的代码如下所示:
class StatsView(DetailView):
""" Create statistics pageview """
template_name = 'app/stats.html'
def get(self, request):
return render(request, self.template_name, context)
def set_if_not_none(self, mapping, key, value):
if value is not None:
if len(value) != 0:
mapping[key] = value
def get_context_data(self, **kwargs):
return context_data
Like this, get_context_data()
function doesn't work, but when I set get()
in comment, it works fine. 这样, get_context_data()
函数不起作用,但是当我在注释中设置get()
时,它可以正常工作。 I think there is a small misunderstanding from myself. 我认为自己有一个小误会。
Maybe I don't use the good django generic display view or maybe it's not possible to use get()
and get_context_data()
together in the same class ? 也许我没有使用好的django通用显示视图,或者不可能在同一类中一起使用get()
和get_context_data()
?
Thank you 谢谢
I read the Django documentation but I would like to get explanations from you 我阅读了Django文档,但我想向您解释
EDIT: 编辑:
I'm trying to pass querysets from get()
method to get_context_data()
. 我试图将查询集从get()
方法传递给get_context_data()
。 Then I removed get()
method, I changed DetailView
by TemplateView
and it works with just get_context_data()
. 然后我删除了get()
方法,通过TemplateView
更改了DetailView
,它仅与get_context_data()
。 But how I can add a "skeleton" without get()
method ? 但是,如何在没有get()
方法的情况下添加“骨架”呢?
I'm trying to pass querysets from get() method to get_context_data() 我正在尝试将查询集从get()方法传递给get_context_data()
class StatsView(DetailView):
""" Create statistics pageview """
template_name = 'app/stats.html'
def get(self, request, *args, **kwargs):
queryset = SampleModel.objects.all()
return render(request, self.template_name, context=self.get_context_data(queryset=queryset))
def set_if_not_none(self, mapping, key, value):
if value is not None:
if len(value) != 0:
mapping[key] = value
def get_context_data(self, **kwargs): qs = kwargs.get('queryset') # do something
If your overriding get_context_data()
method, it's advaisable to call the super()
method as 如果您重写的get_context_data()
方法,建议将super()
方法调用为
class StatsView(DetailView):
# your code
def get_context_data(self, **kwargs): data = super(StatsView, self).get_context_data(**kwargs) data.update({"foo": "bar"}) return data
I would like to get some indicators about
get()
andget_context_data()
我想获得一些有关get()
和get_context_data()
I think it's nicely answered here already , When to use get, get_queryset, get_context_data in Django? 我认为这已经很好回答了, 何时在Django中使用get,get_queryset,get_context_data?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.