← Back to Django Course | Chapter 11: Static Files & Media Handling | Lesson 2 of 8

Configuring STATIC_URL

STATIC_URL is the web address prefix your browser uses to ask for pictures and style sheets, like a street name that tells the mail carrier which neighborhood to deliver to.

Setting STATIC_URL in settings.py

STATIC_URL is a required setting that tells Django (and the browser) what URL prefix static file requests should use.

Note: A new Django project sets this to 'static/' by default — you rarely need to change it.

Example: Setting STATIC_URL in settings.py

STATIC_URL is a required setting that tells Django (and the browser) what URL prefix static file requests should use.

markup
# settings.py
STATIC_URL = 'static/'
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

STATIC_URL vs STATIC_ROOT

STATIC_URL is the public-facing URL prefix; STATIC_ROOT is the absolute filesystem path collectstatic copies files into for production serving. They serve different purposes and are both needed for deployment.

Warning: Never set STATIC_ROOT equal to a directory you also list in STATICFILES_DIRS — collectstatic can overwrite source files.

Example: STATIC_URL vs STATIC_ROOT

STATIC_URL is the public-facing URL prefix; STATIC_ROOT is the absolute filesystem path collectstatic copies files into for production serving. They serve different purposes and are both needed for deployment.

markup
# settings.py
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Referencing STATIC_URL Safely

Rather than typing /static/ directly in a template, use {% static %} so the URL stays correct even if STATIC_URL changes later.

Example: Referencing STATIC_URL Safely

Rather than typing /static/ directly in a template, use {% static %} so the URL stays correct even if STATIC_URL changes later.

markup
{% load static %}
<link rel="stylesheet" href="{% static 'myapp/style.css' %}">
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Setting STATIC_URL without the trailing slash, producing malformed URLs like /staticstyle.css.
  2. Confusing STATIC_URL (the URL prefix browsers use) with STATIC_ROOT (the server-side folder collectstatic writes to).
  3. Changing STATIC_URL after templates already hardcode /static/ paths instead of using the {% static %} tag.
Chapter Summary
  • STATIC_URL sets the URL prefix used to reference static files in the browser.
  • It must end with a trailing slash, e.g. 'static/'.
  • STATIC_URL is separate from STATIC_ROOT, which is a filesystem path used only during deployment.
  • Templates should reference STATIC_URL indirectly through the {% static %} tag rather than hardcoding it.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.