[Fixed]-How to use images loaded with webpack in django templates?

1👍

How about passing correct path in context data? In Django view you are passing context data to the template. You can use regular expressions to find the name of file with hash. Let say that your images are in the directory which variable called STATIC_ROOT links to (place where all static files are). First, you need to find files:

from yourproject.SETTINGS import STATIC_ROOT
all_files = os.listdir(STATIC_ROOT)

Let say that name of the file you want is picture.png, and it was changed to picture-asd12edaq.png Then, find correct file name using regex or simple in operator:

for file in all_files:
    if 'picture' in file and '.png' in file:
        context['src'] = file
        break

Then in template use simple <img src="{% static {{src}} %}" ... />

Leave a comment