Django Static Files
Overview[edit]
Collection of information about how to manage static files with Django.
Settings[edit]
Best practices[edit]
Static file locations[edit]
Static files associated with 3rd-party apps are initially located wherever the app is installed. Django's staticfiles library will locate those static files without running django-admin collectstatic.
In a production environment, django-admin collectstatic is run before starting the Django app because staticfiles is not safe in a production environment.
Place app-specific static resources in the app's directory, e.g. myapp/static/myapp/images/myapp-bg.png. When looking up static resources, Django will place the first file that matches a name. Placing the file inside the app's directory effectively namespaces it.
Referencing static files in templates[edit]
Reference the asset in Django templates using the static template tag:
<link rel="stylesheet" type="text/css" href="{% static 'myapp/app.css' %}">
Or with the static function in Jinja2 templates:
<link rel="stylesheet" type="text/css" href="{{ static('myapp/app.css') }}">
Notes[edit]
See also[edit]
- Writing Your First Django App, Part 6 - Django documentation
- Managing Static Files - Django documentation
- Deploying Static Files - Django documentation
- Static Files Settings - Django documentation
- django.contrib.staticfiles app - Django documentation