Help with django filters - django

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.

Related

How to test if it is a list or a value in a Django template?

In a template in Django I am importing a list of dicts and in one of the keys (tested) I can have either a single value or a list of values depending on the case.
My context dict in the template looks something like this:
context_dicts.append({'url': p.url,
'type_': p.type,
'error': error,
'tested': p.tested})
In the html template I want to if test the tested key to do something if it is a single value and something else if it's a list. So when looping through the dicts, if I use {% if value|length > 1% } it will give me the string size of the value when it's just a value and the length of the list when it's a list. How can I test the if to tell me if it's a "list of one value" or more?
Welcome to SO!
Storing possibly different kind of data in a single variables looks cumbersome to me. It makes your logic less easy to understand and may be prone to errors.
I think the best is to always store a list, but possibly a list with a single element. In the template, you could then do:
{% if tested.count == 1 %}
do stuff with {{ tested.0 }} value
{% else %}
do stuff with {{ tested }} list
{% endif %}

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.

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