0

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.

3 Answers 3

1

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' %}">
Sign up to request clarification or add additional context in comments.

10 Comments

I added it and It didn't change anything
Can you show, how this line looks in the source of the browser? (In the browser: right click -> view page source)
``` <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- css --> <link rel="stylesheet" href="stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/…" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link rel="stylesheet" type="text/css" href="/static/HomePage/style.css"> <title>Results</title> </head> ```
So, in your project app folder, there is a folder static/HomePage which contains the file style.css?
yup, HomePage/static/HomePage/style.css
|
0

Try to put a dot in the path.

like ./HomePage/style.css

1 Comment

It also didnt help
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.