# correct string is mishandled by django

**URL:** <https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094>\
**Category:** Templates & Frontend\
**Created:** [July 28, 2025, 6:17pm UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094 "2025-07-28T18:17:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![onetoucheddy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onetoucheddy/32/29223_2.png) [@onetoucheddy](https://forum.djangoproject.com/u/onetoucheddy)\
**Post date:** [July 28, 2025, 6:17pm UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094/1 "2025-07-28T18:17:25Z")

</div>

my module generates:

```auto
>>> 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:

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

```

In html I have :

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

```

What is going on? What am i missing?

---

<div class="post-metadata">

**Author:** ![nanorepublica](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nanorepublica/32/14211_2.png) [@nanorepublica](https://forum.djangoproject.com/u/nanorepublica)\
**Post date:** [July 28, 2025, 6:36pm UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094/2 "2025-07-28T18:36:14Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![onetoucheddy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onetoucheddy/32/29223_2.png) [@onetoucheddy](https://forum.djangoproject.com/u/onetoucheddy)\
**Post date:** [July 28, 2025, 9:40pm UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094/3 "2025-07-28T21:40:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nanorepublica](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nanorepublica/32/14211_2.png) [@nanorepublica](https://forum.djangoproject.com/u/nanorepublica)\
**Post date:** [July 29, 2025, 7:56am UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094/4 "2025-07-29T07:56:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![onetoucheddy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onetoucheddy/32/29223_2.png) [@onetoucheddy](https://forum.djangoproject.com/u/onetoucheddy)\
**Post date:** [July 29, 2025, 12:55pm UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094/5 "2025-07-29T12:55:51Z")

</div>

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:

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

```

in the template:

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

```

What I get:

```auto
<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.

---

<div class="post-metadata">

**Author:** ![nanorepublica](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nanorepublica/32/14211_2.png) [@nanorepublica](https://forum.djangoproject.com/u/nanorepublica)\
**Post date:** [July 29, 2025, 1:02pm UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094/6 "2025-07-29T13:02:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![onetoucheddy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onetoucheddy/32/29223_2.png) [@onetoucheddy](https://forum.djangoproject.com/u/onetoucheddy)\
**Post date:** [July 29, 2025, 1:04pm UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094/7 "2025-07-29T13:04:11Z")

</div>

Sorry it is mark\_safe(pic). (not trusted\_string) It is may last trial.  
This is what I get with mark\_safe(pic)

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

```

---

<div class="post-metadata">

**Author:** ![onetoucheddy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onetoucheddy/32/29223_2.png) [@onetoucheddy](https://forum.djangoproject.com/u/onetoucheddy)\
**Post date:** [July 29, 2025, 1:24pm UTC](https://forum.djangoproject.com/t/correct-string-is-mishandled-by-django/42094/8 "2025-07-29T13:24:32Z")

</div>

> [@nanorepublica](#):
>
> “{% static pic %}”

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.
