Flask-SQLAlchemy number of results in query - flask

I was wondering if anyone knows how to output the number of results from a query in a flask template.
Example view code:
products = Product.query.all()
In the template it would be handy to be able to just do:
{{ products.count() }}
Is there anyway to do this already, or does anyone have a filter written that does this?
Cheers,

Your products template variable is a regular list. You can use the length filter to gets its size:
{{ products|length }}
But if you are working with paginated results then this will give you the size of one page. If you want the size of the entire query then you have to call count() on the query object. For example:
product_count = Product.query.count()
Then pass this to the template as an additional argument. Or if you prefer you can pass the query object to the template and call count() from there:
{{ product_query.count() }}

you can use len(products) ,
it is standart python function and works for me

Related

Length of list in jinja template, where the list is the result of a DBSession.query

How do I find the length of a list of objects in a jinja template when the list of objects has been created by querying a database?
I thought There are {{ items|length }} items in this category. would work, but items is a result of:
items = db_session.query(Item).filter_by(category_id=category.id)
and I get the error
TypeError: object of type 'Query' has no len()
Obviously, I could calculate the length separately and pass into render_template(), but I wondered if there was a better way?
Any help from the community would be much appreciated :)
items object is not a list yet, it is an unprocessed Query object as you see in the error. You can use Query.all() method to get a list of items:
items = db_session.query(Item).filter_by(category_id=category.id).all()
After that length filter can be applicable.
Use {{ items | count }} in jinja template
Try to add loopcontrol extension
app.jinja_env.add_extension('jinja2.ext.loopcontrols')
Then
{% for item in items %}
{{loop.length}}
{% break %}
{% endfor %}

Django - iterate through 2 objects synchronously in the template form

How do I iterate through 2 objects synchronously in the template form?
I have 2 objects in my views; one object is products, the other prices. Both are lists.
I want to cycle through both products and prices at the same time in the templates form.
Usually in python I would just make a simple while loop as such:
n = 0
while n < len(products):
print products[n], prices[n]
n+=1
I haven't found anything similar in the templates form yet.
Any help would be appreciated.
You could write a custom template filter to get the nth item in the list, but this is what I would do:
Zip the 2 lists together in the view:
products_prices = zip(products, prices)
Then loop through that looped list in the template:
{% for product, prices in products_prices %}
{{ product }}, {{ price }}
{% endfor %}
You might want to use forloop.counter, docs at https://docs.djangoproject.com/en/dev/ref/templates/builtins/
The built in template tags don't have an easy way to do this that I can recall. The simplest approach will be to zip the two lists together in your view, then provide the zipped data structure in the context. You could also write a custom zip template tag, if you're feeling adventurous.

How to format a number with specific number of digits?

I need to show a list like: 1,2,3,etc but show it like: 0001,0001,0003, etc
Exists a template filter in Django to to that?
Simplifying karthikr's answer, you can leverage one of the already provided django template tags to accomplish this:
{{ number|stringformat:"04d" }}

django template: iterate and print just items with index for example 3,6,9

Is there is some good way to iterate through list in django template and print just items with index which is for example 3,6,9 and so on...
Let's imagine I have list like this:
({'title':'first'}, {'title':'second'}, {'title':'third'}, .... {'title':'ninth '})
And in template I want to see just:
third
sixth
ninth
I know how to pass variable to template, I construct in a view, but just need the way how to iterate through them in template and print just what I want.
Or I need to construct list (or dictionary maybe) somehow differently? Using latest Django.
Thanks,
Ignas
The slice filter will allow you to slice a sequence just as you would in Python.
{{ mylist|slice:"2::3" }}

Help with django filters

I am have a list of "Entries"
I want to get the first entry in the list's "title".
In the template I tried writing
{{ Entries|first.title }}
But it results in an error.
Is there a way to achieve this without writing a loop or doing it in the view?
Assuming you passed a list of Entries to the template under the name 'Entries',
{{ Entries.0.title }}
will give you the title of the first entry in the list. The first filter isn't the shortest path to where you want to go in this case.