使用静态文件应用程序在Django中应用css的问题 [英] Problem applying css in Django with static-files app

查看:100
本文介绍了使用静态文件应用程序在Django中应用css的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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" >


推荐答案


我的style.css位于
... / Templates / site_media / static / css

My style.css is located in .../Templates/site_media/static/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的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆