我有两个模型:广告和横幅
当我使用“通用视图”DetailView时我怎样才能同时带两个模型下面的代码只带一个广告
我的网址.py
url(r'^(?P<pk>\d+)/$', DetailView.as_view(
model = Advertisment,
context_object_name = 'advertisment',
), name='cars-advertisment-detail'),
当然可以,只需覆盖即可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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)