Django ListView - 上下文没有更新

时间:2014-05-21 10:11:33

标签: django django-views django-class-based-views class-based-views

views.py

class PaginatorView(_LanguageMixin, ListView):
    context_object_name = 'concepts'

    #some custom functions like _filter_by_first_letter   

    def get_queryset(self):
        # some logic here ...

        all_concepts = self._filter_by_letter(self.concepts, letters, startswith)
        #letters and startswith are obtained from the logic above

        print all_concepts
        return all_concepts

    def get_context_data(self, **kwargs):
        context = super(PaginatorView, self).get_context_data(**kwargs)
        print context[self.context_object_name]
        context.update({
            'letters': [(l[0], self._letter_exists(context[self.context_object_name], l)) for l in self.all_letters],
            'letter': self.letter_index,
            'get_params': self.request.GET.urlencode(),
        })

        return context

print all_concepts语句正确打印我的所有概念。所以一切都在这里工作得很好。然后,我返回all_concepts

此时不应将all_concepts添加到context_object_name指定的密钥下的上下文中?即,context['concepts']应填充all_concepts

如果是这样,get_context_data内的print语句什么都不打印。这表明我没有更新上下文。

当我以前使用DetailView时,get_object函数正在更新context_object_name引用的上下文。(即上下文[context_object_name]填充了get_object返回的对象) 't get_queryset对ListView做同样的事情吗?

_LanguageMixin也在 views.py 中定义,但与我的问题不太相关。只是将它包含在这里供您查看

class _LanguageMixin(object):

    def dispatch(self, request, *args, **kwargs):
        self.langcode = kwargs.pop("langcode")
        self.language = get_object_or_404(Language, pk=self.langcode)
        return super(_LanguageMixin, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(_LanguageMixin, self).get_context_data(**kwargs)
        context.update({"language": self.language,
                        "languages": Language.objects.values_list('code',
                                                                  flat=True)})
        return context

[EDIT1]

如果相反我保存all_conceptsself.all_concepts=...,然后我使用self.all_concepts代替context[self.contex_object_name],一切正常。

[EDIT2]

我从未实例化PaginatorView。它仅用于扩展目的。在这里你可以看到我如何扩展它。 self.concepts帮助我在父类的all_concepts中找到get_queryset(PaginatorView)

class AlphabeticView(PaginatorView):
    template_name = "alphabetic_listings.html"
    model = Property

    def get_queryset(self):
        self.concepts = (
            self.model.objects.filter(
                name='prefLabel',
                language__code=self.langcode,
            )
            .extra(select={'name': 'value',
                           'id': 'concept_id'},
                   order_by=['name'])
            .values('id', 'name')
        )
        super(AlphabeticView, self).get_queryset()

1 个答案:

答案 0 :(得分:1)

get_context_data中的print语句打印为空,因为变量context_object_name为空。您应该尝试print context[self.context_object_name]

编辑:为了回应您的更正,请尝试

print context[self.get_context_object_name(self.get_queryset())]

get_context_object_name docs

编辑2:响应您的第二次编辑,它正在打印的原因是“无”#39;是因为您没有从get_queryset的{​​{1}}方法返回。将该方法中的最后一行更改为

AlphabeticView