0

In my flaskblog.py file i have following dictionay:

facts = [
{   'logo': 'img/hex.png',
    'exchange':'binance',
    'link': 'https://binance.com',
    'volume': '345789',
    'users24h': '23456'
},
{   'logo':'img/wolf.png',
    'exchange':'kucoin',
    'link': 'https://kucoin.com',
    'volume':'123458000',
    'users24h': '17000'
},
{   'logo':'img/wolf.png',
    'exchange':'hitbtc',
    'link': 'https://hitbtc.com',
    'volume':'345890',
    'users24h': '500'
}
]

in my eth.html file i have following:

{% extends "layout2.html"%}
{% block content%}
<br>
<br>
<div class="container">


    <table id="dtBasicExample" class="table table-striped table-bordered table-sm" cellspacing="0" width="80%">
        <thead>
            <tr>
                <th>Logo</th>
                <th>Exchange</th>
                <th>Link</th>
                <th>volume</th>
                <th>Users 24h</th>
            <tr>
        </thead>
        <tbody>
            {% for fact in facts %}
            <tr>
                <td>{{ fact.logo }}</td>
                <td>{{ fact.exchange }}</td>
                <td>{{ fact.link }}</td>
                <td>{{ fact.volume }}</td>
                <td>{{ fact.users24h }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    </div>
    
    {% endblock content %}

When i run it it only prints out the table headings like: Logo, exchange, link etc.. but not actually the content of the dictionary

How can I fix this?

I tried

2
  • 1
    Are you adding the facts in the render_template view function as a parameter?
    – netlemon
    Jul 8, 2020 at 11:18
  • no, i didnt and thatswhy it didnt work- thanks dude Jul 8, 2020 at 11:22

1 Answer 1

0

You are passing a dictionary to html file, not an object. You will have to refer to them with following notation just as you refer a normal dictionary with its keys:

<tbody>
{% for fact in facts %}
    <tr>
         <td>{{ fact['logo'] }}</td>
         <td>{{ fact['exchange'] }}</td>
         <td>{{ fact['link'] }}</td>
         <td>{{ fact['volume'] }}</td>
         <td>{{ fact['users24h'] }}</td>
    </tr>
{% endfor %}
</tbody>

I hope this helps you.

8
  • Have you passed the facts dictionary to the file?
    – dewDevil
    Jul 8, 2020 at 11:21
  • no i didnt do that, now it works- but why does fact.link and fact.volume work but not fact.users24h? when i changed it to fact['users24h] it works- whats the difference? Jul 8, 2020 at 11:25
  • but why arent the links clickable? Jul 8, 2020 at 11:29
  • You just have inserted values from dictionary in the table cell. For them to be clickable as link, you need to surround it with <a> tags. For users24h, it might not be working because variable contains numbers and letters both, but inserting in quotes refers to that particular key that you are referring.
    – dewDevil
    Jul 8, 2020 at 11:32
  • got it thanks- however i have a last question since you already here: I use mdbootstrap for the table design but it doesnt render correctly like the search bar etc dont show up and once i have more than in my case 10 items the page doesnt become scrollable Jul 8, 2020 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.