简体   繁体   中英

Linking up CSS (and static files in general) to html in Django

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

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.

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