简体   繁体   English

在Django中将CSS(通常是静态文件)链接到html

[英]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. 我的html文件未读取我的CSS文件-我尝试搜索有关此问题的所有相关问题,但仍无法读取CSS文件。 Here's what I have: 这是我所拥有的:

settings.py 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 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 我的“ static”和“ templates”文件夹位于同一级别,并且main.css位于static / css / main.css

Edit: If it helps, my bootstrap links are being recognized, only main.css is not. 编辑:如果有帮助,我的引导程序链接将被识别,只有main.css无法识别。

Update: Removed STATICFILES_DIRS from settings.py and added STATIC_ROOT = os.path.join(BASE_DIR, "form/static") as above. 更新:如上所述,从settings.py中删除了STATICFILES_DIRS,并添加了STATIC_ROOT = os.path.join(BASE_DIR,“ form / static”)。

My file structure: 我的文件结构:

app
  app
  - settings.py
  form
  - static
    - css
      - main.css
  - templates
    - form
      - index.html

Try running python manage.py collectstatic . 尝试运行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. 这将从您的STATICFILES_DIRS收集静态文件,并将其放入STATIC_ROOT ,其中staticfiles应用程序将在其中寻找为它们提供服务的文件。

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. 如果您的Django应用在其他服务器(例如nginx,apache,uwsgi或gunicorn)后面运行,则服务器的配置也可以更改文件的提供方式。 Have a look at Django's managing static files documentation for more info. 有关更多信息,请参阅Django的管理静态文件文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM