0

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 ?

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.