trying to use a variable inside a static content tag

383 views
Skip to first unread message

Lyman Hurd

unread,
Jul 20, 2019, 7:31:52 AM7/20/19
to Django users
Greetings.  I am writing a card game and I can load an image when I give a fixed value such as:

<img src="{% static "app/images/ah.png" alt="ah" style="width:69px;height:106px;">

but I would actually like part o fthis to be determined by a tag (there is a for loop setting values) but when I write:

<img src="{% static "app/images/{{ card }}.png" %}"" alt="{{ card }}" style="width:69px;height:106px;">

however when this is rendered into HTML the tag {{ card }} ha sbeen URL-escaped:

<img src"=/static/%7B%7B%20card%20%7D%7D"" alt="KC.png" style="width:69px;height:106px;">

How can I render a static image based on the contents of a tag?


Cheers,

Lyman Hurd


Lyman Hurd

unread,
Jul 27, 2019, 5:25:25 PM7/27/19
to Django users
A helpful user in the Python #django slack channel showed me this answer:

Sithembewena L. Dube

unread,
Jul 27, 2019, 7:07:10 PM7/27/19
to django...@googlegroups.com
Interesting.

Kind regards,
Lloyd


Sent with Shift

--
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/6706118e-f64a-4c6d-8bc6-c558d48640f5%40googlegroups.com.

Sipum Mishra

unread,
Jul 28, 2019, 12:44:03 AM7/28/19
to django...@googlegroups.com
Hi Lyman,

PFB. 

Configure the following setting in your setting.py

STATIC_ROOT = os.path.join(BASE_DIR,"static_files")
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

load staticfiles tag to template 

{% load staticfiles %}

and then use in src attribute of img HTML tag as you did. 


Reply all
Reply to author
Forward
0 new messages