Search⌘ K

Using Static Files in Django

Explore how to serve static files in Django by setting up directories, configuring settings, and injecting images into templates. This lesson helps you manage stylesheets, scripts, and images effectively to enhance your web applications.

So far, we have used templates to insert simple text. But we don’t always just want text. Sometimes, we want to send other types of media, too. For example, say we wanted to return a user’s photo. For this kind of purpose, we use static files.

What are static files?

Static files are files that clients download as they are from the server or files that get served statically, i.e. with no modification. These static files could include stylesheets, JavaScript files, and images. Now, let’s see how to serve static files.

For this example, we will be using the code we got from the Code Templates in Django lesson.

We need to perform the following steps:

Step 1: Create a static directory

First, we will create a directory by the name of static in the outermost first_project directory. Inside this directory, we will create a first_app ...