我有两个模型:广告和横幅
当我使用"通用视图"时DetailView如何同时带两个模型下面的代码只带来一个广告
我的url.py
url(r'^(?P<pk>\d+)/$', DetailView.as_view(
model = Advertisment,
context_object_name = 'advertisment',
), name='cars-advertisment-detail'),
Run Code Online (Sandbox Code Playgroud)
当然,只需覆盖get_context_data以向上下文添加内容.
path('<int:pk>/', YourDetailView.as_view(), name='cars-advertisment-detail'),
class YourDetailView(DetailView):
context_object_name = 'advertisment'
model = Advertisement
def get_context_data(self, **kwargs):
"""
This has been overridden to add `car` to the template context,
now you can use {{ car }} within the template
"""
context = super().get_context_data(**kwargs)
context['car'] = Car.objects.get(registration='DK52 WLG')
return context
Run Code Online (Sandbox Code Playgroud)