问题描述
我使用Django,我无法让我的style.css工作。每次加载我正在工作的页面,没有应用CSS。我还得到这个在终端GET /css/style.css HTTP / 1.1404 2239
I am using Django and I can't get my style.css to work. Every time I load the page I am working on, no css is applied. Also I get this in the terminal "GET /css/style.css HTTP/1.1" 404 2239
我的style.css位于... / Templates / site_media / static / css
My style.css is located in .../Templates/site_media/static/css
我在我的INSTALLED_APPS中也有djago.contrib.staticfiles
I also have djago.contrib.staticfiles in my INSTALLED_APPS
settings.py
I have this in my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = "/Templates/site_media/",
这是我在我的模板中加载css
This is what I have in my template to load the css
<link href="{{STATIC_URL}}css/style.css" rel="stylesheet" type="text/css" >
推荐答案
不匹配
<link href="{{STATIC_URL}}css/style.css" rel="stylesheet" type="text/css" >
您需要将您的链接作为:
you would need to have your link as:
<link href="{{STATIC_URL}}static/css/style.css" rel="stylesheet" type="text/css" >
编辑使用STATICFILES_DIRS(thx Yuji)的绝对路径:
Edit Use absolute paths for STATICFILES_DIRS (thx Yuji):
settings.py:
settings.py:
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_URL = '/static/'
STATICFILES_DIRS = ((os.path.join(SITE_ROOT,'Templates/site-media'),)
请确保您的:
TEMPLATE_CONTEXT_PROCESSORS
有'django.core.context_processors.static',
这篇关于使用静态文件应用程序在Django中应用css的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!