Reputation: 334
I have a flask application. In one view, I am directly doing query on html page using jinja2 as shown below and it works as well
<td> {{ pendlist.query.filter_by(bill_user=users.id).first().bill_balance}} </td>
But is this a good practice to put query like this ? What will be the performance impact on this ?
Upvotes: 0
Views: 630
Reputation: 612
According to the Jinga documentation:
Without a doubt you should try to remove as much logic from templates as possible. Source: FAQ Jinga
Then you shouldn't. Because you don't have control over the result. Jinga is not cut out for this. It doesn't implement all the operator expression. You may slow down the process. Just because it works doesn't mean it should be done.
It is more reasonable to do that:
# The logic is done upstream.
bill_balance = pendlist.query.filter_by(bill_user=users.id).first().bill_balance
# bill_balance is now just a simple value.
# You can check here if bill_balance whether or not this is correct.
# It may look like an empty list/string/None if it is not in your database.
# And you don't necessarily want to post it like that.
return render_template("my_template.html", bill_balance=bill_balance)
Upvotes: 1