When you want to reference a static file such as a CSS file in the HTML of a Django template, you have to be careful about the reference source. If you use it only in the local environment, you can use the absolute path, but if you want to keep it in the project for team development etc., you have to write it differently.
As far as I know, there are two ways to reference static files.
Work environment: macOS Sierra version 10.12.5 Python 2.7.10 Django 1.8.1
With the default settings, if you create a static directory under the application you use, Django will read it.
my_app
└── static
└── my_app
├── css
└── js
If you want to put it somewhere else, add the path to STATICFILES_DIRS in the config file so that you can refer to that directory and it will search for the file.
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Be careful especially if you want to change the css on the Django admin screen. Since base.css etc. used on the admin screen is referenced by Django in Python installed on the PC, it is treated separately from the project file. If you want to change the admin screen in your project, you'll need to copy the template and CSS from within Django so you can see them.
References http://d.hatena.ne.jp/shinriyo/20130814/p4
Recommended Posts