django print only one value in for loop in template - python

I want to display only one value from for loop in template.
Let's say I have this:
{% for category in categories %}
{{category.name}}
See All
{% endfor %}
If I have 5 category then See All in being printed 5 times. How can I only print it once..
Thanx in adnvance..

You should have a main page with all your categories in which you will send it context['categories']
And if you don't need to have a link between your categories in detail just send the current category in the views.py :
context['category']
EDIT:
If all you want to do is break in the loop you can't in django template but you can use slice :
{% for category in categories|slice:":1" %}
It will just go through the loop once

You have to limit the object and send that object to template
tempalte_var['content'] = Categories.objects.all()[:5]

Not the best way but check this:
{% for category in categories %}
{% if categories|length > 1 %}
See All
{% else %}
{{categories[1].name}}
{{category.name}}
{% endif %}
{% endfor %}

This code will print the first element in Category
{% for category in categories %}
{% if categories | first %}
{{category.name}}
See All
{% endif %}
{% endfor %}

Related

How to get forloop.counter inside two forloops

I have a for loop inside another for loop and im trying to get the index (forloop.counter) from the inner loop. Django is for some reason giving me only the outer index.
{% for category in categories %}
{% for product in products %}
{% if product.category.name == category.name %}
<p>This is the amount of products inside this category: {{ forloop.counter }}<p>
{% endif %}
{% endfor %}
{% endfor %}
You can use forloop.parentloop to get to the outer forloop, so in this case {{forloop.parentloop.counter}}. {{forloop.counter}} gives me the inner loop already.
Return to the category view (e.g. in views.py) and add an annotation:
from django.db.models import Count
categories = Category.objects.annotate(num_products=Count('product'))
Remember to include the annotation in this view's context:
context.update({'categories': categories})
return render(request, 'myapp/categories.html', context)
Now, tweak your template to show the counts:
{% for category in categories %}
<p>This is the amount of products inside this category: {{ category.num_products }}<p>
{% endfor %}

Django iterate queryset just n number of times in templates

My issue is quite simple. I am using Django taggit.
I want to iterate this only 2 times. Means to display only 4 tags in templates.
{% for tag in data.tags.all %}
{{tag}}
{% endfor %}
I have tried this, but it is not making any sense:
{% for tag in data.tags.all|ljust:"2" %}
{{tag}}
{% endfor %}
Can anyone suggest how can I achieve it?
You can make use of the |slice template filter [Django-doc]:
{% for tag in data.tags.all|slice:':2' %}
{{ tag }}
{% endfor %}

Django ListView, split list into categories in template, by modifying a "current_category" variable

I don't really know how to phrase my question, but what I'm trying to do is add a title between items to split them into obvious categories on my template. I would like to do this:
{# set variable "current_category" to an empty value first #}
{% for item in items %}
{% if item.category != current_category %}
{{ current_category = item.category }} {# <-- How to do that? #}
<h1>{{ item.category }}</h1>
{% endif %}
<p>{{ item.name }}</p>
{% endfor %}
Then end up with:
<h1>Cat 1</h1>
<p>item</p>
<p>item</p>
...
<h1>Cat 2</h1>
<p>item</p>
..
I saw on similar answers that there are things called custom filters and simple tags but it seems really complicated for something very simple. Is this really the only way to do that?
NOTE: I already ordered the items by category of course
I think you're looking for Django's regroup template tag, which does exactly what you're asking for.
{% regroup items by category as grouped_items %}
{% for category, item_list in grouped_items %}
<h1>{{ category }}</h1>
{% for item in item_list %}
<p>{{ item.name }}</p>
{% endfor %}
{% endfor %}

include a template inside another but with extend

Here is what i want
tmpl1.jinja
{% for x in List %}
{% set User = List[x] %}
{% include 'tmpl2.jinja' %}
{% endfor %}
tmpl2.jinja
{% extends "tmpl3.jinja" %}
{% block link %}
<a>share</a>
{% endblock link %}
tmpl3.jinja
User.name
{% block link %}
{% endblock link %}
Basically i have a user block that exists across site with only the action(one or more link but with quiet a few html like image etc) changing. What can i do.
Thanks
For the template part everything looks absolutely ok and there should not be problem if what you do is what you showed.
Is Your list is dict() or actually list()?
Because your problem is here:
{% for x in List %}
{% set User = List[x] %}
This syntax will work only if List is dictionary.
In case of list you should write:
{% for x in List %}
{% set User = x %}

Why doesn't Django recognize this in the template

I'd like to show my top level categories if the parent is equal to one. The topcats is a queryset that contains category items. However the code starred below is not working. It is not finding any cat items with parent = 1. Any idea why?
{% for cat in topcats %}
**{% if cat.parent == 1 %}**
<h3>{{ cat.category }}</h3>
{% for each in topcats %}
{% if each.parent == cat.id %}
<h5>{{ each }}</h5>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Note: parent is a TreeForeignKey in the database
I'd say cat.parent is a model object.
This should work for you:
{% if cat.parent.pk == 1 %}
However, this is quite hackish, as the parent node's id does not necessarily have a value of 1. The is_root_node() method is a better approach:
{% if cat.parent.is_root_node %}

Categories

Resources