correct string is mishandled by django

my module generates:

>>> from assemble_src import get_pic
>>> get_pic("levis_white.png")
"{%static 'tshirts/images/levis_white.png' %}"

which is what I want in html as src in the img tag. the file name comes from a postgres table. so don’t mind the name of the file which is different. I place the string in row.1
This is what django makes of it:

<img src="{%static" &#x27;tshirts="" images="" print_of_love.png&#x27;="" %}="" style="width: 100px; height: 100px;" class="img-thumbnail" alt="...">

In html I have :

<img src={{row.1}} ...>

What is going on? What am i missing?

The Django template language will not parse a template twice, it would expect {{ row.1 }} to output HTML not another template language stanza.

However if you are getting a filename dynamically then I would recommend reading up on Django MEDIA files which are to do with user uploaded content (be those users and admin or a visitor to your site)

Thank you for your reply, but this is not what I’m looking for. When I paste the output of my module in html, it works. When I use a view, the string is changed into nonsense. How can I make the view put the string in html without changing it. I probably need something like htmlencode or so.

Could we take a step back and perhaps explain what you are trying to achieve overall?

1.Get a filename from postgress
2. create a string to be used in a template img tag as src.
3. pass a variable with the string to the template
4. display an image (the image is in the statc images dir)

Generated string in the view:

pic= "{% static 'print_of_love.png' %}"
pic= trusted_string(pic)

in the template:

<img src={{ pic }} style=...>

What I get:

<img src="{%" static="" 'print_of_love.png'="" %}="" style="width: 100px; height: 100px;" class="img-thumbnail" alt="...">

I want the unchanged string from pic to parsed in the template
Tried a dozen other methods, all fail.

Right ok. So there are 2 issues (or misunderstandings) here.

  1. When dealing with dynamic images/files that are referenced in the database an ImageField along with media settings ought to be the preferred solution generally
  2. Django does not parse a template twice, hence the result you are getting as you are returning Django template code which is not valid HTML

"{% static 'print_of_love.png' %}" this is Django template code not HTML
{{ pic }} this is Django template code not HTML

Perhaps you want this?
"{% static pic %}" where pic is the path to be passed to the static tag?
or {{ pic }} needs to just return the absolute path to the image (which is what an ImageField would do)?

Sorry it is mark_safe(pic). (not trusted_string) It is may last trial.
This is what I get with mark_safe(pic)

<img src="{%" static="" 'print_of_love.png'="" %}="" style="width: 100px; height: 100px;" class="img-thumbnail" alt="...">

this one did it. I got confused because enclosed pic in curly brackets, and that didn’t work. then I got lost trying to get the complete string in pic.

Thanks for your help.

1 Like