3

My html file isn't reading my css file - I've tried searching for all related questions on this issue, but still can't get the css file to be read. Here's what I have:

settings.py

import os
import os.path

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "form/static")
)

Beginning of index.html

{% load staticfiles %}
<!DOCTYPE html>
<html leng="en">
    <head>
        <title>Test</title>

        <link rel="stylesheet" 
       href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" 
       href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-
        theme.min.css">
        <link rel="stylesheet" href="{% static 'css/main.css' %}" />
        </head>

My "static" and "templates" folder are on the same level, and main.css is at static/css/main.css

Edit: If it helps, my bootstrap links are being recognized, only main.css is not.

Update: Removed STATICFILES_DIRS from settings.py and added STATIC_ROOT = os.path.join(BASE_DIR, "form/static") as above.

My file structure:

app
  app
  - settings.py
  form
  - static
    - css
      - main.css
  - templates
    - form
      - index.html
3
  • show your project structure Commented Feb 16, 2018 at 2:08
  • can you try putting {% load static %} at the top of the template and let us know if that changes anything? Commented Feb 16, 2018 at 2:54
  • 2
    I did - at the top of index.html. I tried both {% load static %} and {% load staticfiles %} Commented Feb 16, 2018 at 3:21

2 Answers 2

0

Try running python manage.py collectstatic. This collects the static files from your STATICFILES_DIRS, and puts them in STATIC_ROOT, where the staticfiles app will look for them to serve them.

If your Django app is running behind another server, like nginx, apache, uwsgi or gunicorn, the configuration of your server can also alter how files are served. Have a look at Django's managing static files documentation for more info.

Sign up to request clarification or add additional context in comments.

5 Comments

I ran that and it gave me an error File "manage.py", line 14 ) from exc ^ SyntaxError: invalid syntax Also, in settings.py above, I don't have a STATIC_ROOT variable - am I supposed to have one?
I also read here stackoverflow.com/questions/8687927/… that " STATICFILES_DIRS should serve as additional dirs for static files.If you put all your css/js/images into the APP_NAME/static/APP_NAME folder,then there is no need to specify STATICFILES_DIRS "
Yes, you should have a `STATIC_ROOT. Also, I'm not sure why you're getting a SyntaxError when running manage.py, as that file is automatically generated by Django. Unless you've somehow changed it.
And yep, that's true, you don't need `STATICFILES_DIRS``
I've successfully run collectstatic, and there's no difference. I had to remove STATICFILES_DIRS as it was exactly the same as STATIC_ROOT and terminal gave this error: ERRORS: ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
0

Is form an app? If yes, then you need to register this app in the INSTALLED_APP setting. Next to all the other elements in the list, just add a new element 'form' Only then {% load static %} will work.

Hope this helps!

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.