django - 如何将数据添加到 DetailView 中的上下文对象?

标签 django django-generic-views

我需要在 Django 中编写一个 DetailView。我实现了这个功能。但是,我需要添加更多数据以及上下文对象。我将如何实现这一目标。

我的一般观点是:

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

我需要在上下文对象中再添加一个变量:
response = list_categories(storeId)

最佳答案

如何使用 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

关于django - 如何将数据添加到 DetailView 中的上下文对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50596961/

相关文章:

django - Django get_prep_value() 中的自定义字段无效

python - django icontains 搜索 postgres 数据库

Python/Django 调试 : print model's containing data

Django 部分模板响应

django - 在 Django 模板中访问 RequestContext?

python - UpdateView 不会保存表单,按保存后出现 404 错误

python - Django - 是否使用通用 View ?

django - Django 中的 'article_set' 来自哪里?

python - Django 1.6,如何为CreateView设置字段默认值

python - ListAPIView 中的 Django Rest Framework 序列化错误