将样式变量传递给html标签Django的background属性中的url()

时间:2018-11-12 16:05:52

标签: python html django

我正在尝试将静态图像的位置传递给jumbotron div标签的style属性中包含的背景图像的url()属性。

我尝试了一些不同的操作,如下所示:

这是在我的park_detail.html中

{% extends 'full_base.html' %}
{% load tags %}
{% load static %}

{% block main_content %}
<div class = "jumbotron" style ="background-image:linear-gradient(rgba(255,255,255,.6), rgba(255,255,255,.6)), url({% static {{park.imageloc}} %});background-size:100%;background-position:left center; background-repeat:no-repeat">

此尝试返回错误:

TemplateSyntaxError at /waittimes/park/2/
Could not parse the remainder: '{{park.imageloc}}' from '{{park.imageloc}}' 

我也试图以此作为底线:

<div class = "jumbotron" style ="background-image:linear-gradient(rgba(255,255,255,.6), rgba(255,255,255,.6)), url({% static '{{park.imageloc}}' %});background-size:100%;background-position:left center; background-repeat:no-repeat">

,它在以下位置找不到图像:

/static/%7B%7Bpark.imageloc%7D%7D

预先感谢

1 个答案:

答案 0 :(得分:0)

尝试:

{% static park.imageloc %}

如:

<div class = "jumbotron" style ="background-image:linear-gradient(rgba(255,255,255,.6), rgba(255,255,255,.6)), url({% static park.imageloc %});background-size:100%;background-position:left center; background-repeat:no-repeat">