开发者

Changing the background image with Django template tags

开发者 https://www.devze.com 2023-03-13 08:23 出处:网络
Solved it: I forgot to escape the quotes. I am trying to change a div\'s background image using Django tags, but I am having problems:

Solved it: I forgot to escape the quotes.

I am trying to change a div's background image using Django tags, but I am having problems:

 <div class="inner" style="background-image: url("{{ dogs.0.id }}.jpg");">

The tag that displays the dog's ID correctly because I use it elsewhere and it displays the right information. Am I using style correctly?

Edit: For some reason,开发者_JS百科 it removes all of the slashes from my URL. So https://www.something.com/dog.jpg becomes https: www.something.com dog.jpg


Yes, but offcourse the image needs to exist in the current path you are in since you are using relative url's. Plus you need to set a width and a height for the . Use firebug or Chrome inspector to see if it works and polish up your HTML/CSS knowledge ;)


Edit: For some reason, it removes all of the slashes from my URL. So https://www.something.com/dog.jpg becomes https: www.something.com dog.jpg

you can use "safe" filter to avoid this if your dogs.0.id is https://www.something.com/dog

code like this:

<div class="inner" style="background-image: url("{{ dog.0.id | safe }}.jpg");">


Try the following to dispaly background image in your template in style you need to use following code for inline-css.

<div class="inner" style="background-image: url({% static "image-path/background.jpg" %}");>
0

精彩评论

暂无评论...
验证码 换一张
取 消