How to reference static files in a Django project

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

Put it in the application directory

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

Define a list of directories you want to reference

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

How to reference static files in a Django project
How to create a Rest Api in Django
How to upload files in Django generic class view
Steps to create a Django project
How to handle static files when deploying to production with Django
How to generate a query using the IN operator in Django
How to display a specified column of files in Linux (awk)
How to delete expired sessions in Django
How to get a stacktrace in python
How to deploy a Django app on heroku in just 5 minutes
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
Control files to build in Golang project
How to read CSV files in Pandas
How to get a namespaced view name from a URL (path_info) in Django
How to display Django static files correctly under Amazon Linux2 + Apache environment
How to embed a variable in a python string
Summary of how to import files in Python 3
How to develop a cart app with Django
How to create a JSON file in Python
How to run some script regularly in Django
How to implement a gradient picker in Houdini
How to check / extract files in RPM package
How to notify a Discord channel in Python
Configure a module with multiple files in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory
How to Mock a Public function in Pytest
Start a Django project
How to count the number of elements in Django and output to a template
How to get a list of files in the same directory with python
How to specify a schema in Django's database settings
How to convert / restore a string with [] in python
How to get multiple model objects randomly in Django
How to deploy a Django application on Alibaba Cloud
[Python] How to expand variables in a character string
A memorandum on how to use keras.preprocessing.image in Keras
How to load files in Google Drive with Google Colaboratory
How to build a Django (python) environment on docker
How to display DataFrame as a table in Markdown
How to use variables in systemd Unit definition files
How to download files from Selenium in Python in Chrome
How to add page numbers to PDF files (in Python)
How to change static directory from default in Flask
How to use Decorator in Django and how to make it
To myself as a Django beginner (1) --Create a project app--
How to execute a command using subprocess in Python
How to run Django on IIS on a Windows server
2 ways to read all csv files in a folder
[Linux] How to put your IP in a variable
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
How to use fixture in Django to populate sample data associated with a user model
How to call a function
Shell to create django project
How to hack a terminal
How to develop in Python
Deploy django project to heroku
How to write custom validations in the Django REST Framework
How to slice a block multiple array from a multiple array in Python
How to use Laravel-like ORM / query builder Orator in Django