olofom
olofom

Reputation: 6491

Django get the static files URL in view

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"

Upvotes: 175

Views: 103949

Answers (8)

dyve
dyve

Reputation: 6033

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

Upvotes: 345

Weilory
Weilory

Reputation: 3121

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

Upvotes: 0

Kenial
Kenial

Reputation: 2550

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

Upvotes: 94

devaerial
devaerial

Reputation: 2201

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

Upvotes: 41

Max Malysh
Max Malysh

Reputation: 31635

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

Upvotes: 11

Mesut Tasci
Mesut Tasci

Reputation: 3130

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'

Upvotes: 8

Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13763

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

Upvotes: 10

David Lam
David Lam

Reputation: 4948

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

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

Upvotes: 16

Related Questions