Django get the static files URL in view

DjangoDjango ViewsDjango Staticfiles

Django Problem Overview


I'm using reportlab pdfgen to create a PDF. In the PDF there is an image created by drawImage. For this I either need the URL to an image or the path to an image in the view. I managed to build the URL but how would I get the local path to the image?

How I get the URL:

prefix = 'https://' if request.is_secure() else 'http://'
image_url = prefix + request.get_host() + STATIC_URL + "images/logo_80.png"

Django Solutions


Solution 1 - Django

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

Solution 2 - Django

EDIT: If you're on Django >=3.0, refer to https://stackoverflow.com/questions/11721818/django-get-the-static-files-url-in-view/59355195#59355195 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')

Solution 3 - Django

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')

Solution 4 - Django

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

from django.contrib.staticfiles.storage import staticfiles_storage
staticfiles_storage.url(path)

Solution 5 - Django

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+.

Solution 6 - Django

@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)

Solution 7 - Django

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'

Solution 8 - Django

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionolofomView Question on Stackoverflow
Solution 1 - DjangodyveView Answer on Stackoverflow
Solution 2 - DjangoKenialView Answer on Stackoverflow
Solution 3 - DjangodevaerialView Answer on Stackoverflow
Solution 4 - DjangoDavid LamView Answer on Stackoverflow
Solution 5 - DjangoMax MalyshView Answer on Stackoverflow
Solution 6 - DjangoJahongir RahmonovView Answer on Stackoverflow
Solution 7 - DjangoMesut TasciView Answer on Stackoverflow
Solution 8 - DjangoWeiloryView Answer on Stackoverflow