简体   繁体   中英

Django Static Files - CSS File doesnt load

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.

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' %}">

Try to put a dot in the path.

like ./HomePage/style.css

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM