How to format a number with specific number of digits? - django

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" }}

Related

Django template filter to convert a string into single item list

Is there a template filter that converts a string into a list with just one item.
I did see make_list but that creates a list of individual characters of the string.
Illustration: {{ "string"|filter }} gives ["string"]
As far as I know, there is not a builtin template filter for this. You can write your own according to docs. I don't understand the aim very well but you can always create your own template filter to achieve custom actions.

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.

Flask-SQLAlchemy number of results in query

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

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.