How to insert a variable in a file path in Django template?

Dear Pro Django, the lack of experience forces me to ask for help in the following question:

There is a project in the application to 'countries' in which each user enters certain data. The PYGAL module depending on the introduced data builds a picture that the template is loaded as:

{% load static %} 

<img src="{% static 'countries/World.svg' %}">

'countries/World.cvg' is the path. Picture created by pygal, is called the World.svg, and it lies in the folder static/countries/

That is the question - the function to present processed data entered by the user, handed them over to the PYGAL, it draws each user a unique picture. And, accordingly, the template Django should be considered {{ user }} and display it in a unique picture from many others.

For each individual user of the template needs to load different pictures. How to implement it? Because I have to specify in the template the path to the picture, including her name. I can make PYGAL for each user to save images with a different name, but how to get the template to understand that for specific {{ user }} need to ship it here is his picture? How to insert a variable image name (depend on user) tag

<img src="{% static 'countries/World.svg' %}">

June 3rd 19 at 21:00
1 answer
June 3rd 19 at 21:02
the tag accepts a static string or a variable.

If a variable in the context is not:
{% with var1='user/123.svg' %}
<img src="{% static var1 %}">
{% endwith %}

if there is then just {% static varname %}

PS I would put the logic of selecting images from a template in the vyuha, or the method in the user model
Tempting, but I can't. Pygal may be considered a user and assign the saved image name like Oleg.svg thus reserving picture to a specific user. But then it does in the template do not bind to {{ user }} and the name of Oleg.svg. Construction type
{% with var1='{{ user }}.svg' %}
<img src="{% static var1 %}">
{% endwith %}

Don'T work!

How to insert the variable {{ user }} into a variable var1 and pass the template to shipped to Oleg Oleg.svg, for John - John.svg, and to Olga - Olga.svg ?? - derick.Lang commented on June 3rd 19 at 21:05
I do not quite understand how a bunch of pygal and pictures.
judging by
there are several suitable options.
1 - render_to_file. to upload a picture file and use in subsequent normal file.
it should be used only when the result (picture) is very static and rarely change.
but in this case, the file should upload to media dir, but not in static, and even better in the field FileField.

2 - render_django_response. come for the dynamics. to make a view which will dynamically generate the image depending on the parameter, and in img src you can simply specify the path to this view in the form
<img src="{% url "generate_image" user %}">

3 - render_data_uri. put the result in context and in the template just <img src="{{ img_data_uri }}"> - maybelle97 commented on June 3rd 19 at 21:08
thank you. The second option worked great! Noted solution. - derick.Lang commented on June 3rd 19 at 21:11
really I don't understand why you are against statics in the first case? What will change the user data will change the picture static, the browser displays the changed image, what is bad? Why media shipping or model change? - derick.Lang commented on June 3rd 19 at 21:14
, static files only created by the developer and does not change during program execution (because it is usually put in the git), files generated during user interaction media (because it's not usually put in git, and macapat like database and other things) - Garnett_Deckow commented on June 3rd 19 at 21:17
thanks for the clarification, will do so now. - derick.Lang commented on June 3rd 19 at 21:20

Find more questions by tags PythonDjango