Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django通过ListView类将参数传递到网页_Django_Python 3.x - Fatal编程技术网

Django通过ListView类将参数传递到网页

Django通过ListView类将参数传递到网页,django,python-3.x,Django,Python 3.x,我有URL.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('{% s

我有URL.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的上下文添加额外的变量:

然后更新url模式以使用新视图:

url(r'^index', PostListView.as_view())

是否要从服务器传递参数值?ListView将为您提供对象列表。如何搜索变量background_image?该变量应该来自何处?在开始使用ListView之前,我使用了render(请求'personal/index.html',{'background_image':'personal/img/home bg.jpg'))谢谢你帮了我的忙,顺便说一句,你忘了“return context”语句谢谢,我添加了return语句。
url(r'^index', PostListView.as_view())