简体   繁体   中英

Pass Model Variable to url() in background attribute in style html tag Django

I am trying to pass the location of a static image to the url() attribute of a background image contained in the style property of a jumbotron div tag.

I've tried a few different things as shown below:

This is in my 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">

This attempt returns an error:

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

I also attempted this as the bottom line:

<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">

and it could not find the image at location:

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

Thanks in advance

Try:

{% static park.imageloc %}

as in:

<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">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM