1

If there are no records in my portfolio, I do not want to show the table in index.html

So, I tried as a parameter to pass a variable empty with value to index.html, where it was checked {% if {{ empty }} == "False" %}

Pseudo-code:

if no records:
    return render_template("index.html", empty = "True", cash_current = usd(cash_current[0]['cash']), grand_total = usd(grand_total))
if records:
    return render_template("index.html", empty = "False", stocks = stock_info, cash_current = usd(cash_current[0]['cash']), grand_total = usd(grand_total))

I tried the code below but unsuccessfully

{% block main %}
    {% if {{ empty }} == "False" %}
    <table>
    <tr>
        <th>Symbol</th>
        <th>Name</th>
        <th>Shares</th>
        <th>Price</th>
        <th>TOTAL</th>
    </tr>
    {% for stock in stocks %}
        <tr>
            <td> {{ stock.symbol }} </td>
            <td> {{ stock.name }} </td>
            <td> {{ stock.quantity }} </td>
            <td> {{ stock.price }} </td>
            <td> {{ stock.total }} </td>
        </tr>    
    {% endfor %}
</table>
    {% endif %}
{% endblock %}

The main idea is that if there are no records in portfolio, do not display table headings

How is it possible to implement this idea?

Any help is greatly appreciated ;)

Thanks!

1
{% block main %}
    {% if empty == "False" %}
             // TODO Somethig
        <p>NOOO</p>
    {% endif %}
{% endblock %}

Easy fix

I was trying to to pass a variable empty as a parameter with value to index.html, where it was checked {% if {{ empty }} == "False" %}`

The idea is good, but it did not work. Apparently instead of {% if {{ empty }} == "False" %} the correct way to check is to do so {% if empty == "False" %}

4
  • Please include some words as explanation in your answer, and not just code. – ChrisG Jun 22 '17 at 7:42
  • @ChrisG Explanation added – wingedRuslan Jun 23 '17 at 20:43
  • do you need to change code in application.py also? like change 'return render_template("index.html", empty = "False", stocks = stock_info, cash_current = usd(cash_current[0]['cash']), grand_total = usd(grand_total))' to ' empty = "No"? – Michael Zhang Jun 26 '17 at 22:32
  • @MichaelZhang Sure, the value of the variable empty is up to you, but this value must match the value in IF statement. So yes, if I check empty = "No", I must pass empty = "No" here return render_template("index.html", empty = "No" – wingedRuslan Jun 26 '17 at 23:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .