variable inside a template

25 views
Skip to first unread message

luca72.b...@gmail.com

unread,
Jun 16, 2022, 7:09:15 AM6/16/22
to Django users
Hello i have a list like:
mylist =  ['1,png','2.png',3.jpj']
in the template i do as follow:
{% for o in mylist %}
        <ul>
        {% if "png" in o %}
        {{ o }}
        <li><img width="770" height="1500" alt="" src="{% static "polls/{{o}}" %}" class=""></li>
        {% else %}
            <li>{{o}}</li>
        </ul>
        {% endif %}
        {% endfor %}
in the template i see the file name but it never render, if i loock the source code i see that in the <img tag i have this line
<li><img width="770" height="1500" alt="" src="/static_files/polls/%7Bo%7D" class=""></li>
So i never get the correct file name.
How is the correct way to get the name of the variable o inside the img tag
Thanks

Sencer Hamarat

unread,
Jun 16, 2022, 10:42:56 AM6/16/22
to django...@googlegroups.com
Don use 'o' as a template variable in the template function

Use {% static 'poll/'|add:o %} instead.

16 Haz 2022 Per 14:09 tarihinde luca72.b...@gmail.com <luca72.b...@gmail.com> şunu yazdı:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b7e47034-7029-476a-bb08-a5e780c4b95bn%40googlegroups.com.

Larry Martell

unread,
Jun 16, 2022, 12:20:25 PM6/16/22
to django...@googlegroups.com
mylist has 1,png not 1.png
^
Could that be the issue?

Antonis Christofides

unread,
Jun 16, 2022, 2:24:55 PM6/16/22
to django...@googlegroups.com

What you are trying to do is very unusual, and any solution to exactly what you are asking is going to be an ugly hack. So, why do you want to do this? What are these static files that you have in mylist?

Reply all
Reply to author
Forward
0 new messages