Problem applying css in Django with static-files app

懵懂的女人 提交于 2019-12-02 19:34:40

问题


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

My style.css is located in .../Templates/site_media/static/css

I also have djago.contrib.staticfiles in my INSTALLED_APPS

I have this in my settings.py

STATIC_URL = '/static/'


STATICFILES_DIRS = "/Templates/site_media/",

This is what I have in my template to load the css

<link href="{{STATIC_URL}}css/style.css" rel="stylesheet" type="text/css" >

回答1:


My style.css is located in .../Templates/site_media/static/css

doesnt match

<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" >

Edit Use absolute paths for STATICFILES_DIRS (thx Yuji):

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'),)

make sure that your:

TEMPLATE_CONTEXT_PROCESSORS has 'django.core.context_processors.static',



来源:https://stackoverflow.com/questions/5396552/problem-applying-css-in-django-with-static-files-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!