0

I am facing a strange issue with the Django application on my local, the file is available in the static directory.

The following is in the settings.py

STATIC_ROOT = SITE_ROOT + '/static/'
STATIC_URL = '/static/'

this is how I am trying to include style file

<link href="/static/msc.css?v=1.2" rel="stylesheet" type="text/css" />

the following is added in urls.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

The file exist in the static directory, but it is not loading, I am facing a 404 error in console for the URL: http://localhost:8000/static/msc.css?v=1.2

please help me in this regard thanks.

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Hafiz Siddiq
  • 671
  • 3
  • 9
  • 23
  • What is your `STATICFILES_DIRS`? – NKSM Mar 11 '21 at 10:13
  • 1
    I highly recommend you to look through the official django tutorials https://docs.djangoproject.com/en/3.1/intro/tutorial06/ – Ersain Mar 11 '21 at 11:04
  • take a look at this https://stackoverflow.com/questions/4919600/django-project-root-self-discovery and this https://stackoverflow.com/questions/66437690/django-html-template-cant-find-static-css-and-js-files/66439076#66439076 – Ivan Starostin Mar 11 '21 at 11:26
  • please also note that in my case only this style sheet is not being loaded rest of the things are loading properly – Hafiz Siddiq Mar 11 '21 at 12:51

1 Answers1

0

You are not doing things the Django way.

When working with static files in Django

a) you need to use {% load static %} at the very beginning of your template

b) The link to static file is created using the following pattern

<link href="{% static 'path/to/style.css' %}" rel="stylesheet" type="text/css" />

Suppose that I have a directory in my django project static/css/style.css, then

<link href="{% static 'css/style.css' %}" rel="stylesheet" type="text/css" />
alv2017
  • 752
  • 5
  • 14