我有urls.py
urlpatterns = [
url(r'^index',
ListView.as_view(queryset=Post.objects.all().order_by("-date")[:4], template_name="personal/index.html")),
]
还有一个模板文件header.html,其中有下一行
< header class="intro-header" style="background-image: url('{% static background_image %}')">
. . .
</header>
如您所见,我试图将背景图片设置为header,该路径保存在background_image变量中,我想知道如何传递此参数
有什么办法可以做,还是我做错了?
您可以通过将视图子类化并覆盖来为Django CBV的上下文添加额外的变量get_context_data
:
class PostListView(ListView):
queryset = Post.objects.all().order_by("-date")[:4]
template_name = "personal/index.html"
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
context['background_image'] = 'personal/img/home-bg.jpg'
return context
然后更新您的网址格式以使用新视图:
url(r'^index', PostListView.as_view())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句