本文介绍了如何在DetailView中向上下文对象添加数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Django中编写一个DetailView.我实现了此功能.但是,我需要与上下文对象一起添加更多数据.我将如何实现这一目标.
I need to write a DetailView in Django. I achieved this functionality. However, I need to add some more data along with the context object. How will I achieve this.
我的通用视图是:
class AppDetailsView(generic.DetailView):
model = Application
template_name = 'appstore/pages/app.html'
context_object_name = 'app'
我需要再向上下文对象添加一个变量:
I need to add one more variable to the context object:
response = list_categories(storeId)
推荐答案
如何使用get_context_data
How about using get_context_data
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
这篇关于如何在DetailView中向上下文对象添加数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!