title: Render Flask tables depending on the model
body: <p>My app contains different db.models eg. (Customers Cameras ...). I would like to fill my template &quot;tables.html&quot; with data dynamically depending on the model called.</p>
  <p>I succeeded to enter the headers but I struggle to populate the content of the table.</p>
  <p>Normally the data would be populated by {{ data.cust_name }} or {{ data.cam_name }} etc. but obviously in my case it doesn't work.</p>
  <p>I would be very grateful if someone could point me in the right direction?</p>
  <p>models.py</p>
  <pre><code>class Customers(db.Model):
      cust_id = db.Column(db.Integer, primary_key=True)
      cust_name = db.Column(db.String, unique=True, nullable=False) ....
  
   def __repr__(self):
          return f&quot;Customers( '{self.cust_name}','{self.cust_address}','{self.cust_zip}','{self.cust_city}','{self.cust_phone}','{self.cust_email}','{self.cust_active}',' {self.cust_timestamp.strftime('%d/%m/%Y %H:%M:%S')}')&quot;
  </code></pre>
  <p>routes.py</p>
  <pre><code> table = val
      match table:
          case &quot;Customers&quot;:
              tableName = Customers
              tbl_prefix = &quot;cust_&quot;
          case &quot;Cameras&quot;:
              tableName = Cameras
              tbl_prefix = &quot;cam_&quot;
          case &quot;Endpicts&quot;:
              tableName = Endpicts
              tbl_prefix = &quot;ep_&quot;
          case &quot;Extcalpoints&quot;:
              tableName = Extcalpoints
              tbl_prefix = &quot;ecp_&quot;
          case &quot;Extrinsics&quot;:
              tableName = Extrinsics
              tbl_prefix = &quot;ec_&quot;
          case &quot;Intrinsics&quot;:
              tableName = Intrinsics
              tbl_prefix = &quot;ic_&quot;
          case &quot;Pumps&quot;:
              tableName = Pumps
              tbl_prefix = &quot;pmp_&quot;
          case &quot;Rawpicts&quot;:
              tableName = Rawpicts
              tbl_prefix = &quot;rp_&quot;
          case &quot;Stations&quot;:
              tableName = Stations
              tbl_prefix = &quot;pst_&quot;
          case &quot;Settings&quot;:
              tableName = Settings
              tbl_prefix = &quot;set_&quot;
          case &quot;Users&quot;:
              tableName = Users
              tbl_prefix = &quot;usr_&quot;
          case &quot;Visits&quot;:
              tableName = Visits
              tbl_prefix = &quot;vis_&quot;
          case _:
              return redirect(url_for('login')) 
      cur = conn.cursor()
   #column headers
      try:
          columns=tableName.__table__.columns
          headings=[]
          for c in columns:
              heading=c.name.replace(tbl_prefix,&quot;&quot;)
              headings.append(heading)
      except:
          conn.rollback()
  
      #columns
      if tableName == Users:
          try:
              cur.execute(&quot;SELECT * FROM &quot; + tableName +&quot; ORDER BY id&quot;)
              data = cur.fetchall()
          except:
              conn.rollback()
      else:
          try:
              n=[]
              data =  tableName.query.all()
              
          except:
              conn.rollback()
  
  return render_template('tables.html', title=val, prefix=tbl_prefix, headings= headings,data=data, username=current_user.usr_name)
  </code></pre>
  <p>table.html</p>
  <pre><code>...
  &lt;table id=&quot;table&quot; class=&quot;table table-hover&quot;&gt;
              &lt;thead&gt;
                  &lt;tr class=&quot;theader&quot;&gt;
                      {% for header in headings %}
                      &lt;th class=&quot;table-cell&quot;&gt; {{header}} &lt;/th&gt;
  
                      {% endfor %}
                      &lt;th&gt;&lt;/th&gt;
                      &lt;th&gt;&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              
              &lt;tbody class=&quot;tbody&quot;&gt;
                  {% for row in data %}
                  
  
                  &lt;tr class=&quot;table-row&quot; row_id=&quot;{{row[0]}} &quot;&gt;
                      {%for row in data %}
                      
                      
                      &lt;td&gt;
                          {% if row %}
                          &lt;div input readonly=&quot;&quot; class=&quot;row_data&quot; edit_type=&quot;click&quot;&gt; {{ data.cust_name }}&lt;/div&gt;
                          {% else %}
                          &lt;div input readonly=&quot;&quot; class=&quot;row_data&quot; edit_type=&quot;click&quot;&gt; &amp;nbsp;&lt;/div&gt;
                          {% endif %}
                      &lt;/td&gt;
                      {% endfor %}
                      &lt;td&gt;
                          &lt;a id=&quot;btn_edit&quot; class=&quot;btn_edit&quot; row_id=&quot;{{row[0]}}&quot;&gt;&lt;i class=&quot;fa fa-pencil fa-1x&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                          &lt;a id=&quot;btn_save&quot; class=&quot;btn_save&quot; row-id=&quot;{{row[0]}}&quot;&gt;&lt;i class=&quot;fa fa-check fa-1x&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                          &lt;a id=&quot;btn_cancel&quot; class=&quot;btn_cancel&quot; row-id=&quot;{{row[0]}}&quot;&gt;&lt;i class=&quot;fa fa-xmark fa-1x&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                          &lt;a id=&quot;btn_delete&quot; class=&quot;btn_delete&quot; row-id=&quot;{{row[0]}}&quot;&gt;&lt;i class=&quot;fa fa-trash-o fa-1x&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  {% endfor %}
              &lt;/tbody&gt;
          &lt;/table&gt;
  ...
  </code></pre>