Django Static Files: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
No edit summary
 
Line 5: Line 5:


== Settings ==
== Settings ==
== Best practices ==
=== Static file locations ===
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.
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 ===


Reference the asset in Django templates using the `static` template tag:
Reference the asset in Django templates using the `static` template tag:

Latest revision as of 01:32, 12 February 2016

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]