Django Static Files and Templates

Static files like CSS, JavaScript, and fonts are a core piece of any modern web application. They are also typically confusing for Django newcomers since Django provides tremendous flexibility around how these files are used. This tutorial will demonstrate current best practices for configuring static files in both local development and production.

Local Development

For local development the Django web server automatically serves static files and minimal configuration is required. A single Django project often contains multiple apps and by default Django will look within each app for a static directory containing static files.

For example, if one of your apps was called blog and you wanted to add a CSS file to it called base.css, you would need to first create a new directory called blog/static and then add the file within it at the location blog/static/style.css.

However, since most Django projects involve multiple apps and shared static files, it is common on larger projects to instead create a base-level folder typically named static. This can be added from the command line with the mkdir command:

(.venv) $ mkdir static

For demonstration purposes let's also add a base.css file. Assuming you had only just started a new Django project with the startproject command your directory structure would now look like this:

├── django_project
│   ├──
|   ├──
│   ├──
│   ├──
│   └──
├── static
│   ├── base.css

Within the file, near the bottom, there is a single line of configuration for static files called STATIC_URL, which is the location of static files in our project.

# django_project/
STATIC_URL = "static/"

This means that all static files will be stored in the location or http://localhost:8000/static/. And if you wanted to access the base.css file its location would be or http://localhost:8000/static/base.css.

Loading static files into Templates

Loading static files in a template is a two-step process:

There are two main ways to structure templates in a Django project as outlined in this tutorial. Let's assume we are using a templates/base.html file for a Blog project. To add our static base.css file to it we'd start by adding {% load static %} at the top of the file and then use the {% static %} tag with the path to it. Since the STATIC_URL is already set to /static/ we don't need to write the full route out of static/base.css and can instead shorten it to base.css.

<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
  <title>Learn Django</title>
  <link rel="stylesheet" href="{% static 'base.css' %}">

If you save and reload the web page, you'll see the changes. Adding links for either images in an img folder or JavaScript in a js folder would look as follows:

<img src="{% static 'img/path_to_img' %}">
<script src="{% static 'js/base.js' }"></script>


Serving static files in production requires several additional steps and is where the confusion typically arises for Django newcomers. Local development is designed to keep things nice and easy, but it is far from performant. In a production environment it is more efficient to combine all static files in the Django project into one location and serve that a single time.

Django comes with a built-in management command, collectstatic, that does this for us.

We need three more configurations in our file though before collectstatic will work properly. The first is STATICFILES_DIRS which defines additional locations, if any, the staticfiles app should look within when running collectstatic. In our simple example the only location for local files is the static directory so we will set that now.

# django_project/
STATIC_URL = "static/"
STATICFILES_DIRS = [BASE_DIR / "static"]  # new

Next up is STATIC_ROOT which sets the absolute location of these collected files, typically called staticfiles. In other words, when collecstatic is run locally it will combine all available static files, as defined by STATICFILES_DIRS and place them within a directory called staticfiles. This is how we set that configuration.

STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"  # new

The final step is STATICFILES_STORAGE, which is the file storage engine used when collecting static files with the collectstatic command. By default, it is implicitly set to Let's make that explicit for now in our django_project/ file.

STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles" 

Now we can run the command python collectstatic which will create a new staticfiles directory.

(.venv) $ python collectstatic

If you look within it you'll see that staticfiles also has folders for admin (the built-in admin has its own static files), staticfiles.json, and whatever directories are in your static folder.

If you now add a new static file to the static directory it will immediately be available for local usage. It is only for production where the file won't be present unless you run python collectstatic each and every time. For this reason, running collectstatic is typically added to deployment pipelines and is done by default on Heroku.

As a brief recap:


Even though we've configured our Django project to collect static files properly, there's one more step involved which is not included in the official Django docs. That is the configuration of WhiteNoise to serve the static files in production. The first step is to install the latest version of the package:

(.venv) $ python -m pip install whitenoise==6.0.0

Then in the django_project/ file make three changes:

It should look like the following:

    "whitenoise.runserver_nostatic",  # new

    "whitenoise.middleware.WhiteNoiseMiddleware",  # new


STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"

That's it! Run python collectstatic again so that the files are stored using WhiteNoise. And then deploy with confidence to the hosting platform of your choice.


Content Delivery Networks (CDNs) are useful on very high traffic sites where performance is a concern. Rather than serving static files from your Django server you post them on a dedicated CDN network and then call them. The official WhiteNoise documentation has additional instructions on this step.


Configuring static files is a core part of any Django project. If you'd like to see multiple examples of real-world Django projects alongside detailed explanation, check out my book Django For Beginners. The first several chapters can be read for free online.

Join My Newsletter

Subscribe to get the latest tutorials/writings by email.

    No spam. Unsubscribe at any time.