[Django]-Get static image URL in Python code

2👍

You can use the template tag in python code, like this:

from django.templatetags.static import static

full_path = static('img/my_img.png')

This will also take into account use cases where the full static path is not simply the concatenation of settings.STATIC_URL with the relative path. See for instance this example in Django documentation.

1👍

You can always import the settings file

from django.conf import settings
...

if my.condition:
    return ("<img src='%s/img/my_img.png' />" % (settings.STATIC_URL)) + obj.nam
👤rjv

Leave a comment