[Answer]-Django: concatinating strings to create a url to static image in template

1๐Ÿ‘

I know you can use {% static your_url %} but I allways prefer to do like this:

I have my images in myproject/static/img/

My {{STATIC_URL}} (settings.py variable) points to myproject/static

So in a template I usually do: <img src="{{STATIC_URL}}img/image_name.png" />

Or using an ImageField

{% for item in item_list%}
    <img src="{{ item.image.url }}" />
{% endfor %}

In your case you could do:

<img src="{{ MEDIA_URL }}{{ info.personid }}.jpg" />

{{ MEDIA_URL }} already ends with /

  • You need MEDIA_URL to be defined in your settings.py
๐Ÿ‘คAlvaroAV

0๐Ÿ‘

How about:

<img src="{% get_media_prefix %}{{ info.personid }}.jpg"/>
๐Ÿ‘คseddonym

Leave a comment