dictionary sortiert in tabelle ausgeben

Django, Flask, Bottle, WSGI, CGI…
Antworten
Benutzeravatar
nieselfriem
User
Beiträge: 135
Registriert: Sonntag 13. Januar 2013, 16:00

Hallo zusammen,

ich habe folgendes Dictionary:

Code: Alles auswählen

dictionary={'ort1': {'id': 43, 'test1': 'info test1 ', 'prod': 'info prod1', 'test2': 'info test2'}, 'ort2': {'id': 67, 'test1': 'info test1', 'prod': 'info prod', 'test2': 'info test2'},'ort3': {'id': 95, 'test1': 'info test1', 'prod': 'info prod', 'test2': 'info test2'},...}
Dieses möchte ich nun in einer HTML-Tabelle, die ich in einem Jinja-Template definiert habe ausgeben. Leider ist die Reihenfolge der Daten im Dictionary nicht so, wie ich sie in der Tabelle haben möchte.

Nun dachte ich mir, dass ich mittels einer if Bedingung und dem key festlege, in welcher spalte der Tabelle die richtige Info aus dem Dictionary ausgeben wird. Das klapp jedoch nicht so richtig. Ich hätte gern ein Tipp, wie ich dieses Problem lösen könnte
Dazu mal mein nicht funktionierender Versuch. Die Ausgabe in den Spalten erfolgt nach Dictionary Reihenfolge der keys und nicht nach der gewünschten in der Spalte.

Code: Alles auswählen

 <table style="width:100%" id="table">
        <thead>
        <tr>
            <th>Standort</th>
            <th>Test1</th>
            <th>Test2</th>
            <th>PROD</th>
            <th>Id</th>
        </tr>
        </thead>
        <tbody>
{% for standort,infos in versions_data.items() -%}
    <tr>
    <td>{{ standort}}</td>
    {%  for key, info in infos.items()  %}
        {{ key }}
    {% if key == "test1" %}<td>{{ info }}  </td>{% endif %} {% if key == "test2" %}<td>{{ info }}  </td> {% endif %}{% if key == "prod" %}<td>{{ info }}  </td> {% endif %}{% if key == "id" %}<td>{{ info }}  </td>{% endif %}
{%  endfor %}
{%  endfor %}
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>
Danke schon einmal für die Infos.

VG niesel
Benutzeravatar
__blackjack__
User
Beiträge: 14012
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@nieselfriem: Du musst statt der inneren Schleife halt einfach in jede Zelle per Schlüsselzugriff genau das rein schreiben was Du da jeweils haben willst.

Edit: Die äussere ``for``-Schleife hört fälschlicherweise vor dem </tr> auf.

Code: Alles auswählen

    <table style="width:100%;" id="table">
      <thead>
        <tr>
          <th>Standort</th>
          <th>Test1</th>
          <th>Test2</th>
          <th>PROD</th>
          <th>Id</th>
        </tr>
      </thead>
      <tbody>
        {% for standort, infos in versions_data|dictsort -%}
        <tr>
          <td>{{ standort }}</td>
          <td>{{ infos['test1'] }}</td>
          <td>{{ infos['test2'] }}</td>
          <td>{{ infos['prod'] }}</td>
          <td>{{ infos['id'] }}</td>
        </tr>
        {%  endfor %}
      </tbody>
    </table>
“The best book on programming for the layman is »Alice in Wonderland«; but that's because it's the best book on anything for the layman.” — Alan J. Perlis
Benutzeravatar
nieselfriem
User
Beiträge: 135
Registriert: Sonntag 13. Januar 2013, 16:00

Geilo! :D Das ist es. Ich habe es mir fast gedacht, dass ich es mir viel zu kompliziert mache.


VG
Antworten