Configuring STATIC_URL
In this page:
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.
# 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.
# 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.
{% 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. #}
- Setting STATIC_URL without the trailing slash, producing malformed URLs like /staticstyle.css.
- Confusing STATIC_URL (the URL prefix browsers use) with STATIC_ROOT (the server-side folder collectstatic writes to).
- Changing STATIC_URL after templates already hardcode /static/ paths instead of using the {% static %} tag.
- 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.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: