load static in css file

194 views
Skip to first unread message

Chander shekhar

unread,
Jul 30, 2020, 8:37:27 AM7/30/20
to Django users
I am unable to fetch images from css file .
I have used {% load static %}
{% static 'imag.jpg' %}
buti can't fetch.console is saying image not found.

Irfan Khan

unread,
Jul 30, 2020, 8:41:44 AM7/30/20
to django...@googlegroups.com
{% static 'imag.jpg' %}   instead of this try

{% static “images/imag.png” %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/554cbaf1-3b94-40b4-a0f1-efe0bd3f24ceo%40googlegroups.com.

chander shekhar

unread,
Jul 31, 2020, 7:58:48 AM7/31/20
to django...@googlegroups.com
tried this buddy yet it is not working

Regards
Chander Shekhar
(bestcsp)


Yemin Sajid

unread,
Aug 3, 2020, 5:07:29 AM8/3/20
to Django users
This may not work as intended if the CSS files are not rendered using the Django template engine. You have to put the style declaration where you'd need the image in the <style></style> tag in your template code. From there you can use the {% static “images/imag.png” %} syntax.

Obodoma Uzondu Vincent

unread,
Aug 3, 2020, 10:04:12 AM8/3/20
to django...@googlegroups.com
Hello your have to follow the direction where you stored your file . Generally, in Django you create a static folder then maybe an image files where you store the images. You should link the image this way src= "{% static 'images/imag.png' %}"

MAMADOU KANTE

unread,
Aug 24, 2020, 8:08:01 AM8/24/20
to django...@googlegroups.com
Hi Everybody!
I am trying to set a background image using the STYLE tag with django template as follows:
 <div class="background-img-wrapper" style="background-image: url(images/background1.jpg)"></div>
But when I open the HTML page and inspect the page I receive an image with width zero. Can anyone help?
When I use the same syntax on a regular HTML without django templates it works.

Akash Sinha

unread,
Aug 24, 2020, 8:50:26 AM8/24/20
to django...@googlegroups.com

Hi

Create a folder inside ur project as static and keep ur images,css and js files into that folder then in your settings.py file define static path. U need to run python manage.py collect static command on terminal and then you have to link that css file in HTML


Hint: check for Managing static files in official django doc or try stack overflow.com you will get example to setup static path


sapna Choudhary

unread,
Aug 24, 2020, 4:03:00 PM8/24/20
to django...@googlegroups.com
create a directory named 'static' in the current directory. This directory is used to store images/css files, you can load static in template using {%load static%}  this code block and link under image tag like this {% static 'app_name/img.jpg' %}

MAMADOU KANTE

unread,
Aug 24, 2020, 8:16:24 PM8/24/20
to django...@googlegroups.com
I did load static at the top of the file {% load static %}.
Also I linked the CSS file named style.css. Part of the css file was working perfectly. I only had issues with the background image.

RANGA BHARATH JINKA

unread,
Aug 25, 2020, 1:37:45 AM8/25/20
to django...@googlegroups.com
Hi,
     Try this.
     style="background: url(../images/showcase.jpg) no-repeat top center !important;"
    Add these in your settings.py file.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

  STATIC_URL = '/static/'

  STATICFILES_DIRS = [
     os.path.join(BASE_DIR, 'static')
  ]

   MEDIA_URL = '/images/'

    MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
     Also check the correct path of images. Whether the images are loading properly by pasting the image path url in the browser.
     All the best.





--
Thanks and Regards

J. Ranga Bharath
cell: 9110334114

sapna Choudhary

unread,
Aug 25, 2020, 5:08:20 AM8/25/20
to django...@googlegroups.com
The image which you want as background image has to be in static folder.

Reply all
Reply to author
Forward
0 new messages