I\'d like to use an image file as a background-image on Django
. But I do not know how.
First, I read this and tried to write like following this in a css file.
Here is the structure of directories for my project so you can be in context of why i'm using the URL's i am using and so you can adapt it to your project. Root directory is the folder than contains the project and apps. I have 4 apps (contacto, galeria, inicio, mapa) and the "WebCalistenia" which is the folder generated when you create a project. There i have static folder with a child called as the father ("WebCalistenia") which has two childs "/css" and "/img"
enter image description here
This is what worked for me. Firstly you have to {% load static %} on your HTML.
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
...
Now you'll link the CSS to HTML
<link rel="stylesheet" href="{% static 'app_name/css/document.css' %}">
Finally on the css file you will write:
background: url("../img/image_name.jpeg");
base.html in body tag
<body>
{% load static %}
</body>
style.css
#third{
background: url("/static/img/sample.jpeg") 50% 0 no-repeat fixed;
color: white;
height: 650px;
padding: 100px 0 0 0;
}
problem slove
background-image: url('../img/4799045.jpg');
best way I think
Use this:
#third{
background: url("/static/img/sample.jpeg") 50% 0 no-repeat fixed;
color: white;
height: 650px;
padding: 100px 0 0 0;
}
Most probably This will work.Use "/static/" before your image URL and then try them. Thanks
In case there's anyone looking for another way to solve this, you may want to hot-link the image from your server or any image hosting service you have access to. Here's how I solved this:
Here's an example:
https://images.your-host.com/photos/image-path-here.jpeg
hello I had some problems I used pycharmS as tool this how I managed to solve the problem obviously you have to load static in your html file and setup your settings.py for
if all have be done well but the issue is on css file
your image is in folder called img which is under static folder
YOU HAVE TO DO LIKE THIS :
background: Url("../img/myimage.jpeg")
; this is the part of your image
don't put all settings code of the background on the same line
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
YOU USE BROWSER LIKE CHROME TO OPEN YOUR PROJECT I USED MICROSOFT EDGE NO CHANGE ON MY PROJECT HAS BEEN APPLIED SIMULTANEOUS