Working and Configuring Static Files in Django.

Abdullah zulfiqar
Django Unleashed
Published in
4 min readJan 30, 2024

I’ve seen that beginners in Django often get confused about something called ‘statics.’ They ask questions on places like StackOverflow, and get good answers, but these answers don’t always explain what statics are and why they’re needed. I’m here to make it easy for you to understand static files in Django and how to set them up.

Before we dive in, I’m assuming you already know the basics of Django and have a simple project set up.

Objective:

  • Explaining how static actually works in Django.
  • How to setup static files dir and how to serve statics.

What are Static files?

Static are files on your website that you share with users, but they can’t change or delete them. These files can be things like stylesheets (CSS), video/audio files (.mp4, *.mp3, .webm), banners and logos (.png, *.gif, .jpg), or data files like JSON.

What does “serving” even mean?

Serving static files means making certain static files, like CSS (used for styling), available for your website or app to use. But wait which static files? is it confusing? Lets make it more simple.

In a Django project, there are some static files, like CSS styles for the admin panel, that users can’t change and used for rendering the admin interface styles are stored within the “django.contrib.admin” app. Now when you start the server using “runserver” and with “Debug=True” Django’s development server is designed to serve static files for you automatically, including the CSS and other assets required for the admin panel. whether you’re using the development server or handling them in another way “django.contrib.staticfiles” app ensures that all your static files are managed and served correctly (made available) so that it knows where to look for them.

Configuring STATIC_URL, STATICFILES_DIRS, and STATIC_ROOT for Serving Web Template Static Files:

when you create templates for different apps, you often include static files like CSS, logo.png, or JavaScript files to style and enhance your web pages. While Django’s development environment automatically handles static files, including styles for the admin panel, when “Debug” is set to “True,” the situation changes when you have custom templates like the homepage. In such cases, you need to take control of serving these static files.

Now you need to specify where these static files should be stored, how your templates should reference them or from which directory they should be served. This configuration is essential to ensure that your web pages uses the statics correctly when you are in development environment with custom static files for pages like homepage or “Debug” is set to “False” or a production-ready server like Gunicorn is used.

Now open settings.py file and go straight to the STATIC_URL setting, which should be like this.

STATIC_URL = '/static/'

This setting means that your static files will be accessible via URLs like localhost:8000/static/ during development or example.com/static/ in production. Think of it as a reference to your static files, and remember to include a trailing slash ‘/’ at the end.

Next, we need to declare the STATICFILES_DIRS

STATIC_URL = '/static/'   #Location of static files
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]

This informs Django about the location of static files in our project, typically developers stores in a top-level ‘static’ directory. But As a list, STATICFILES_DIRS allows for multiple static directories if needed.

Next, we have to set the STATIC_ROOT

STATIC_ROOT  = os.path.join(BASE_DIR, 'staticfiles')

STATIC_ROOT is the central directory for serving static files in production, often placed within BASE_DIR, named ‘static’. When you run ‘collectstatic’, it gathers and stores all project static files in this directory.

Loading static files in Templates

Open base.html file and at the very top add {% load static % }

{% load static %}
<!DOCTYPE html><html>
<head>
<title>Django Central</title>

This setting allows Django to understand and use special commands like {% static %} in your templates. You can use {% static %} to include different static items, like images or styles, in your web pages.

For example, if I want to use ‘example.css’ or ‘example.js’ to load any javascript or image file in my template, I need set static in this way:

<link rel="stylesheet" href="{% static 'css/example.css' %}">
<img src="{% static 'img/example.png' %}">
<script src="{% static 'scripts/example.js' %}"></script>

Why You Need to Set STATIC_URL, STATICFILES_DIRS, or STATIC_ROOT for Production or When Using Servers Other than Django’s Development Server?

When we turn DEBUG to False, it’s a sign we’re getting ready for a production setup. In this mode, Django’s development server doesn’t automatically handle static files anymore. However, even with DEBUG set to True, if we’re using a different server like Gunicorn, we still have to tell Django where to locate static files.

Automatic serving of static files is mainly built for the admin panel when we’re using Debug=True with Django’s development server. But, remember, it’s not safe or efficient for a production environment, which is why we need to configure static files differently.

Security Issues:

  • Incorrect setup can reveal your project’s file structure, a security risk.
  • It may allow unauthorized access to sensitive files, potentially leading to data breaches.
  • Implementing a Content Security Policy (CSP) becomes challenging, increasing the risk of cross-site scripting (XSS) attacks.

Performance Issues:

  • Inefficient serving slows down page loading.
  • Inefficient serving limits the ability to handle high traffic loads.
  • Lack of caching wastes bandwidth, increasing hosting costs and page loading times.

--

--

Abdullah zulfiqar
Django Unleashed

Software Engineer Python | Full Stack Architect | Python Engineer | Django | Flask | FastAPI | AI | Freelance