Website for allowing data evaluation from submission I'm making a really basic website where users essentially just submit data into some forms and they will be able to review their data in another page. I feel like I'm going to be repeating myself far too much.
This is how I'm planning to display the users records, the trouble I'm having is I have about six more tables of data that is slightly different. So I can't just hard-code a template because the number of columns and the names of the columns is slightly different in each table. I know I could just make six different HTML files but that just seems silly. I feel like I need to somehow loop over the attributes of the models and then somehow display them in the template?
Is there a better way of displaying database data in a template?
class (db.Model):
    __abstract__ = True
    employee_name =  db.Column(db.String(50), nullable=False)
    date_submitted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    comment =  db.Column(db.String(100), nullable=True)


class FridgeFreezerTempTable(Base):
    id = db.Column(db.Integer, primary_key=True)
    unit_name = db.Column(db.String(50), nullable=False)
    unit_type = db.Column(db.String(50), nullable=False)
    temperature = db.Column(db.String(10), nullable=False)
    company_id = db.Column(db.Integer, db.ForeignKey('user.id'),nullable=False)

@app.route(&quot;/unit_temp_records/&lt;int:page_number&gt;&quot;)
def unit_records(page_number):
    &quot;&quot;&quot;
    records = pagiantion object
    page number = the current page
    
    &quot;&quot;&quot;
    records = FridgeFreezerTempTable.query.filter_by(user=current_user).paginate(per_page=5,                             
             page=page_number, error_out=True)
    return render_template(&quot;unit_temp_records.html&quot;, user_records=records, table=table)

{% extends &quot;records.html&quot;%}
&lt;!--This is garbage, find a way to make all below a template.. dir? __dict__???--&gt;
{%block recordcontent%}
&lt;table class=&quot;table table-striped&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th scope=&quot;col&quot;&gt;#&lt;/th&gt;
            &lt;th scope=&quot;col&quot;&gt;Unit Name&lt;/th&gt;
            &lt;th scope=&quot;col&quot;&gt;Unit Type&lt;/th&gt;
            &lt;th scope=&quot;col&quot;&gt;Temperature&lt;/th&gt;
            &lt;th scope=&quot;col&quot;&gt;Employee Name&lt;/th&gt;
            &lt;th scope=&quot;col&quot;&gt;Comment&lt;/th&gt;
            &lt;th scope=&quot;col&quot;&gt;Date Submitted&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;

    &lt;tbody&gt;
        {% for record in user_records.items %}
            
            &lt;tr&gt;
                &lt;th scope=&quot;col&quot;&gt;{{ loop.index }}&lt;/th&gt;
                &lt;td&gt;{{record.unit_name}}&lt;/td&gt;
                &lt;td&gt;{{record.unit_type}}&lt;/td&gt;
                &lt;td&gt;{{record.temperature}}&lt;/td&gt;

                &lt;td&gt;{{record.employee_name}}&lt;/td&gt;
                &lt;td&gt;{{record.comment}}&lt;/th&gt;
                &lt;td&gt;{{record.date_submitted}}&lt;/td&gt;
            &lt;/tr&gt;
        {% endfor %}
    &lt;/tbody&gt;

    &lt;!-- Also garbage (maybe figure out how to put this into records template --&gt;
    {% for page in user_records.iter_pages() %}
    {% if page %}
    &lt;a class=&quot;test&quot; href=&quot;{{url_for('unit_records', page_number=page) }}&quot;&gt;{{ page }}&lt;/a&gt;
    {% else %}
    .
    {% endif %}
    {% endfor %}
&lt;/table&gt;
{% endblock%}
```
Perhaps the templates can be generated by a function.  Pass in a list of the attribute names, and it returns a template string.  Or pass in the table name, and query the column names to make the template.  Then use render_template_string() instead of render_template().

This code is rough and untested, but outlines what I'm thinking:

def column_headers(attrs):
    headers = (f'&lt;th scope="col"&gt;{attr.replace("_"," ").titlecase()}&lt;/th&gt;' 
               for attr in attrs)
    return '\n'.join(headers)

def columns(attrs):
    column_refs = (f'&lt;td&gt;{{record.{attr}}}&lt;/td&gt;' for attr in attrs)
    return '\n'.join(column_refs)

def make_template(attrs):
    template_string = ["""
        {% extends "records.html"%}
        {%block recordcontent%}
        &lt;table class="table table-striped"&gt;
            &lt;thead&gt;
                &lt;tr&gt;
                """,
                column_headers(attrs),
                """
            &lt;/thead&gt;
            &lt;tbody&gt;
                {% for record in user_records.items %}
                &lt;tr&gt;
                    &lt;th scope="col"&gt;{{ loop.index }}&lt;/th&gt;
                    """,
                    columns(attrs),
                    """
                &lt;/tr&gt;
                {% endfor %}
            &lt;/tbody&gt;
        &lt;/table&gt;
        {% endblock%}
        """]
        return '\n'.join(template_string)

FridgeFreezerTemplate = make_template("""
                                      unit_type
                                      temperature
                                      employee_name
                                      comment
                                      date_submitted
                                      """.strip().split())

@app.route("/unit_temp_records/&lt;int:page_number&gt;")
def unit_records(page_number):
    """
    records = pagiantion object
    page number = the current page

    """
    records = FridgeFreezerTempTable.query.filter_by(user=current_user).paginate(per_page=5,                             
             page=page_number, error_out=True)
    return render_template_string(FridgeFreezerTemplate, user_records=records, table=table)