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
ilee
  • 201
  • 4
  • 12

2 Answers2

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.

bigblind
  • 12,539
  • 14
  • 68
  • 123
  • 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? – ilee Feb 16 '18 at 03:48
  • I also read here https://stackoverflow.com/questions/8687927/django-static-static-url-static-root 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` " – ilee Feb 16 '18 at 03:57
  • 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. – bigblind Feb 16 '18 at 03:57
  • And yep, that's true, you don't need `STATICFILES_DIRS`` – bigblind Feb 16 '18 at 03:58
  • 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.` – ilee Feb 16 '18 at 06:02
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!