How can I dynamically render images from an image folder using Jinja and Flask?

I use Flask to learn Python and create a toy application that I would like to do for a while. I have problems with a specific function, which is a standard file upload. I want to do to try to dynamically render an image from my images folder based on a specific model, but I seem to have a problem trying to interpolate a line.

Here is my code:

<h1>List of Employees</h1>

{% if employees %}
    {% for employee in employees: %}
      <p>{{ employee.name }}</p>
      <p>{{ employee.title }}</p>
      <p>{{ employee.email }}</p>
      <p>{{ employee.department }}</p>

      # How do I use Jinja and python to interpolate this so I can 
      # grab the correct image
      <img src="static/images/" + {{employee.profile_image }} alt={{ employee.name }} width="120" height="90">{{ employee.profile_image }}</img>
    {% endfor %}
  {% endif %}

It should be very simple. I was spoiled by ruby ​​and rails .... Help would be appreciated. Thanks.

+4
source share
1 answer

The tag imgshould look like this:

<img src="static/images/{{ employee.profile_image }}" alt={{ employee.name }} width="120" height="90" />

, employee.profile_image - static/images/

profile_image , , Jinja2 default.

<img src="static/images/{{ employee.profile_image | default('profile.jpg') }}" alt={{ employee.name }} width="120" height="90" />
+11

Source: https://habr.com/ru/post/1613253/


All Articles