[Django]-Django get the static files URL in view

337πŸ‘

βœ…

# 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/'

πŸ‘€dyve

94πŸ‘

EDIT: If you’re on Django >=3.0, refer to Django get the static files URL in view instead. This was answered with Django 1.X version.

dyve’s answer is good one, however, if you’re using "cached storage" on your django project and final url paths of the static files should get "hashed"(such as style.aaddd9d8d8d7.css from style.css), then you can’t get a precise url with django.templatetags.static.static(). Instead, you must use template tag from django.contrib.staticfiles to get hashed url.

Additionally, in case of using development server, this template tag method returns non-hashed url, so you can use this code regardless of that the host it is development or production! πŸ™‚

from django.contrib.staticfiles.templatetags.staticfiles import static

# 'css/style.css' file should exist in static path. otherwise, error will occur 
url = static('css/style.css')
πŸ‘€Kenial

35πŸ‘

From Django 3.0 you should use from django.templatetags.static import static:

from django.templatetags.static import static

...

img_url = static('images/logo_80.png')
πŸ‘€devaerial

15πŸ‘

here’s another way! (tested on Django 1.6)

from django.contrib.staticfiles.storage import staticfiles_storage
staticfiles_storage.url(path)
πŸ‘€David Lam

11πŸ‘

Use the default static tag:

from django.templatetags.static import static
static('favicon.ico')

There is another tag in django.contrib.staticfiles.templatetags.staticfiles (as in the accepted answer), but it is deprecated in Django 2.0+.

πŸ‘€Max Malysh

8πŸ‘

@dyve’s answer didn’t work for me in the development server. Instead I solved it with find. Here is the function:

from django.conf import settings
from django.contrib.staticfiles.finders import find
from django.templatetags.static import static

def get_static(path):
    if settings.DEBUG:
        return find(path)
    else:
        return static(path)

7πŸ‘

If you want to get absolute url(including protocol,host and port), you can use request.build_absolute_uri function shown as below:

from django.contrib.staticfiles.storage import staticfiles_storage
self.request.build_absolute_uri(staticfiles_storage.url('my-static-image.png'))
# 'http://localhost:8000/static/my-static-image.png'
πŸ‘€Mesut Tasci

0πŸ‘

In short words you need to get

  • STATIC_URL
  • STATIC_ROOT
  • urlpatterns
  • staticfiles
  • templatetags
  • url parameters

All in the right place, to get this working. In addition, in real time deployment, circumstances vary, which it is very possible that the current setting you spent 3 hours worked out, works on your local machine but the server.

So I adopted the traditional way!!

app
β”œβ”€β”€ static
β”‚   └── json
β”‚       └── data.json
└── views.py

views.py

import os

with open(os.path.abspath(os.getcwd()) + '/app/static/json/data.json', 'r') as f:
    pass
πŸ‘€Weilory

Leave a comment