Django Static Files

From Littledamien Wiki
Revision as of 01:32, 12 February 2016 by Video8 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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]