3

I'm running on localhost but stylesheet isn't working

Here is my project setup:

-main
   -main
   -home
      -templates
         -home
             -index.html
   -static
      -css
         -style.css

My settings.py:

STATICFILES_DIRS = [
    "/static/",
]

STATIC_URL = '/static/'

index.html:

{% load staticfiles %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>My Home Page</title>
    <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
</head>
<body>
<p>My Content</p>
</body>
</html>

Here is what my server outputs: "GET /static/css/style.css HTTP/1.1" 404 1652

What is wrong?

3 Answers 3

2

Okay this change seemed to fix it:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    '/static/',
]
Sign up to request clarification or add additional context in comments.

Comments

0

index.html should be like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
{% load staticfiles %}
<html lang="en">
<head>
    <title>My Home Page</title>
    <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
</head>
<body>
    <p>My Content</p>
</body>
</html>

Comments

0

I was having the same issue because my settings file in Django didn't have the following lines of code, which I added to solve the problem

STATICFILES_DIRS = [
    BASE_DIR / "static",
    '/var/www/static/',
]

see the static files documentation here: https://docs.djangoproject.com/en/3.1/howto/static-files/

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.