django serving static css files

有些话、适合烂在心里 提交于 2019-12-22 00:58:58

问题


I am relatively new to Django development.I have a css file inside a /static/css directory.

When I try to run the url no CSS is applied to my template. the python manage.py runserver window shows following error

[01/Jan/2013 20:00:40] "GET /home/prat/PROJECT_ROOT/SOURCE_ROOT/static/css/Style.css HTTP/1.1" 404 2207

Can someone please point me how to debug this. I have read multiple stackoverflow questions and added the following setting in my settings.py.

PROJECT_R = os.path.abspath(os.path.dirname(__name__))
PROJEECT_R = PROJECT_R + "../"
STATIC_ROOT = os.path.join(PROJECT_R, "static")
STATIC_URL = 'static/'

.
├── manage.py
├── README
├── SOURCE_ROOT
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── static
│   ├── css
│   │   ├── README
│   │   └── Style.css
│   ├── images
│   │   └── README
│   └── js
│       └── README
├── template
│   ├── base.html

回答1:


Here's how I usually go about managing dynamic project root:

from os.path import dirname, realpath, join
PROJECT_ROOT = dirname(realpath(__file__))

And then further below, the static root:

STATIC_ROOT = join(PROJECT_ROOT, 'static/')    

And then you reference static files like so:

{{ STATIC_URL }}css/Style.css

EDIT:

See the documentation for more information.



来源:https://stackoverflow.com/questions/14116157/django-serving-static-css-files

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