代码之家  ›  专栏  ›  技术社区  ›  deltaforce

如何在详细视图中将数据添加到上下文对象中?

  •  1
  • deltaforce  · 技术社区  · 7 年前

    我需要用django编写一个详细视图。我实现了这个功能。但是,我需要添加更多的数据和上下文对象。我将如何实现这一点。

    我的一般观点是:

    class AppDetailsView(generic.DetailView):
        model = Application
        template_name = 'appstore/pages/app.html'
        context_object_name = 'app'
    

    我需要再向上下文对象添加一个变量:

    response = list_categories(storeId)
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   burning    7 年前

    如何使用get_context_数据

    class AppDetailsView(generic.DetailView):
         model = Application
         def get_context_data(self, **kwargs):
            context = super(AppDetailsView, self).get_context_data(**kwargs)
            context['categories'] = list_categories(storeId)
            return context