Home > Back-end >  Django Static Files - CSS File doesnt load
Django Static Files - CSS File doesnt load

Time:11-12

I'm learning Django. I am trying to load a static css file, but it doesn't work. That's part of my base.html:

{% load static %}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- css -->


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


  </head>

and that's part of my settings.py file:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR / "static",
    '/var/www/static/',
]
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
  os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_in_env", "static_root")

Where did I make mistake ?

I have already used python manage.py collectstatic in command prompt

edit: I see that I declareted STATICFILES_DIRS list two times.

CodePudding user response:

How does href look in the browser?

I would guess there is a missing / in the href path.

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

CodePudding user response:

Try to put a dot in the path.

like ./HomePage/style.css

CodePudding user response:

It looks like it is resolving correctly because your source has href="/static/HomePage/style.css"

What happens when you open localhost:8000/static/HomePage/style.css in your browser. Do you get a 404?

And, to be sure, what happens with localhost:8000/HomePage/static/HomePage/style.css

  • Related