Django get the static files URL in view

# Older Django <3.0 (also deprecated in 2.0):
from django.contrib.staticfiles.templatetags.staticfiles import static

# Django 3.0+
from django.templatetags.static import static

url = static('x.jpg')

url now contains '/static/x.jpg', assuming a static path of '/static/'

Leave a Comment