-2

I have checked a links for how to set an image as background for a div. I have tried various ways. But still am going wrong somewhere and would need some guidance...

Below is my directory structure:

frontend
    /frontend (django project)
        settings.py
        .
        .
    /webApp (django app)
        /templates
            /static
                img1.jpg
            home.html

In my home.html I have the following line,

{% load static %}

<div style="background-image: url({% static "/img1.jpg"%}); height: 650px; width: 1920px;">

In settings.py I have,

STATIC_URL = '/static/'

But the image does not appear when the page is displayed. I get the following error.

"GET /static/img1.jpg HTTP/1.1" 404 1803

My directory structure was different and have searched a few questions before to fit my need. I still am getting confused with the urls and assigning url for the background image. Also i want it as a url and not as href to set the background-image. How do I go about this?

nidHi
  • 833
  • 3
  • 10
  • 21

3 Answers3

1

Try to specify STATICFILES_DIRS in settings.py

STATICFILES_DIRS = (
    "<absolute path to static folder>",
)
Anna
  • 523
  • 3
  • 8
0

try add but with proper path:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
perror
  • 7,071
  • 16
  • 58
  • 85
gargi258
  • 829
  • 1
  • 10
  • 16
0

I made a few changes... I shifted the static folder to where my settings.py is present... And remove the / before img1.jpg in the html file... Finally it's working now...

nidHi
  • 833
  • 3
  • 10
  • 21