S
S
shmostertoster2017-01-03 21:51:02
Flask
shmostertoster, 2017-01-03 21:51:02

How to get dictionary values ​​into a table in Flask?

Hello!
How to get values ​​from dictionary to table in HTML?
No matter how I complicate the for loop on the page, it doesn't work. The code prints all the key values ​​from the dictionary.
It is necessary that in 31 cells corresponding to the days of the month values ​​from the dictionary are substituted.
Direct my thoughts in the right direction.
Thank you!
Here is the code:

#!/usr/bin/env python
#!/usr/bin/env python

from flask import Flask, flash, redirect, render_template, \
     request, url_for

app = Flask(__name__)

@app.route('/tabel')
def index():
    return render_template(
        'tabel1.html',
        taba = [{'01': 'О', '04': 'У', '14': 'О', '26': 'У', '09': 'У', '20': 'У', '16': 'У', '02': 'О', '12': 'У',
                '19': 'У', '18': 'У', '07': 'О', '22': 'О', '21': 'У', '03': 'У', '28': 'О', '08': 'О', '25': 'У',
                '05': 'У', '27': 'У', '23': 'У', '17': 'У', '31': 'У', '24': 'У', '15': 'О', '06': 'У', '10': 'У',
                '11': 'У', '30': 'У', '29': 'О', '13': 'У'}])

if __name__=='__main__':
    app.run(debug=True)

Here is the page:
<html>
<head>
<title>Месяц</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
</head>

<table width="100%" border="1" cellspacing="0">
  <tr>
    <td bordercolor="1"></td>
  </tr>
  <tr>
    <td bordercolor="1"><table width="100%" border="1" cellspacing="0">
      <tr bgcolor="#D1D5C1">
        <td><div align="center"><span>1</span></div></td>
        <td><div align="center"><span>2</span></div></td>
        <td><div align="center"><span>3</span></div></td>
        <td><div align="center"><span>4</span></div></td>
        <td><div align="center"><span>5</span></div></td>
      </tr>
    <tr bgcolor="#D1D5C1">
        <td><div align="center"><span>{% for dict_item in taba %}{% for k, v in dict_item.items() %}{{v}}{% endfor %}{% endfor %}</span></div></td>
        <td><div align="center"><span>{% for dict_item in taba %}{% for k, v in dict_item.items() %}{{v}}{% endfor %}{% endfor %}</span></div></td>
        <td><div align="center"><span>{% for dict_item in taba %}{% for k, v in dict_item.items() %}{{v}}{% endfor %}{% endfor %}</span></div></td>
        <td><div align="center"><span>{% for dict_item in taba %}{% for k, v in dict_item.items() %}{{v}}{% endfor %}{% endfor %}</span></div></td>
        <td><div align="center"><span>{% for dict_item in taba %}{% for k, v in dict_item.items() %}{{v}}{% endfor %}{% endfor %}</span></div></td>
      </tr>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VovanZ, 2017-01-03
@shmostertoster

No need to complicate the cycle in depth.
Option 1:
Option 2:

{% for key, value in taba[0].items() %}{{ value }}{% endfor %}

S
shmostertoster, 2017-01-06
@shmostertoster

Here's another question. The HTML file per employee is 400 lines long.
For a unit of 400 people - this will never load.
31 columns for dates + 4 rows (from the list) per employee (full name + work schedule + time sheet + selects (dict) for each date with a no-show type)
Taking into account the fact that the changed values ​​in the select need to be written to the database.
Can you suggest a course of action.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question