Grouping dates in Django templates - django

im trying to group my objects by year only year in datetime field.
{% regroup notificacoes by criados|date:"Y" as criados_list %}
<ul>
{% for x in criados_list %}
<li>
{{ x.grouper }}
<ul>
{% for y in x.list %}
<li>{{ y.slug }} : {{ y.arquivo }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
this was supposed to work, but is not working

Related

How to display N number of Backward relationship in Django templates?

{% for category in categories %}
{% for product in categories.product_set.all %}
<h1> {{ product.name }} </h1>
{% endfor %}
{% endfor %}
I want to show 10 products instead of all in template
There is a slice filter that you can use in templates:
{% for category in categories %}
{% for product in categories.product_set.all|slice:":10" %}
<h1> {{ product.name }} </h1>
{% endfor %}
{% endfor %}

Django template using variable in two blocks

This is my template code:
{% block content %}
{% get_latest as latest_posts %}
<ul>
{% for post in latest_posts %}
<li>
<p>{{ post.title|safe }}</p>
</li>
{% endfor %}
</ul>
{% endblock %}
{% block sidebar %}
<ul>
{% for post in latest_posts %}
<li>
<p>{{ post.title|safe }}</p>
</li>
{% endfor %}
</ul>
{% endblock %}
In the content block the for loop does works, but in the sidebar block I can't use the variable latest_posts. Can anyone help me with this?
The variable's scope is limited to the containing {% block content %}. You can either repeat the declaration inside {% block sidebar %}, or move it up a level so it's outside of {% block content %}.
example
{% get_latest as latest_posts %}
{% block content %}
<ul>
{% for post in latest_posts %}
<li>
<p>{{ post.title|safe }}</p>
</li>
{% endfor %}
</ul>
{% endblock %}
{% block sidebar %}
<ul>
{% for post in latest_posts %}
<li>
<p>{{ post.title|safe }}</p>
</li>
{% endfor %}
</ul>
{% endblock %}
or
{% block content %}
{% get_latest as latest_posts %}
<ul>
{% for post in latest_posts %}
<li>
<p>{{ post.title|safe }}</p>
</li>
{% endfor %}
</ul>
{% endblock %}
{% block sidebar %}
{% get_latest as latest_posts %}
<ul>
{% for post in latest_posts %}
<li>
<p>{{ post.title|safe }}</p>
</li>
{% endfor %}
</ul>
{% endblock %}
You can use the with statement:
Definition:
with Caches a complex variable under a simpler name. This is useful
when accessing an “expensive” method (e.g., one that hits the
database) multiple times.
Wrap your existing block into this with clause and you can safe yourself some queries. also it should solve your problem :-) Just remove the {% get_latest as latest_posts %} line
{% with latest_posts=get_latest %}
{% block content %}
<ul>
{% for post in latest_posts %}
<li>
<p>{{ post.title|safe }}</p>
</li>
{% endfor %}
</ul>
{% endblock %}
{% block sidebar %}
<ul>
{% for post in latest_posts %}
<li>
<p>{{ post.title|safe }}</p>
</li>
{% endfor %}
</ul>
{% endblock %}
{% endwith %}

Cumulative count in recoursetree?

Using django-mppt I want to browse my category hierarchy displaying the ammount of objects related to the current category in any of it's children.
Much like drill_down_for_node in the example shown, but only with the current node childrens...
The optimal would be
{% recursetree obj.get_children cumulative count model.Foreignkey.category in o_count%}
<li>
<h2>{{ node }}</h2>
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }} ({{o_count}})
</ul>
{% endif %}
</li>
{% endrecursetree %}
Any pointers?
Found a function for this on the TreeManager:
https://github.com/django-mptt/django-mptt/blob/master/mptt/managers.py#L250
Add the counts to the queryset in your view:
context['qs_with_related_count'] = model1.objects.add_related_count(
obj.get_children(),
model2,
'category',
'o_count',
True
)
and in template:
{% recursetree qs_with_related_count %}
<li>
<h2>{{ node }} ({{ node.o_count }})</h2>
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
Unfortunately I was trying to use a ManyToManyField as rel_field:
https://github.com/django-mptt/django-mptt/issues/90
but it looks like you are in luck ('category' not 'categories') :)

Django regroup not working as expected

I have the following view in my django application
def ViewSale( request ):
salecur = Sale.objects.filter(user=2).order_by('sale_date')
return render_to_response('myapp/sale.html',{'salecur':salecur})
my template looks like this
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
When i render the page i get the grouper sale_date.grouper printed, but {{ sale.item }} and {{ sale.qty }} in the inner loop shows nothing! Blank.
What am i missing?
Gath
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_date.list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
See the documentation on regroup:
{% regroup %} produces a list of group objects. Each group object has two attributes:
grouper -- the item that was grouped by (e.g., the string "Male" or "Female").
list -- a list of all items in this group (e.g., a list of all people with gender='Male').

Is there a better way to write out my django-treemenu template?

I'm using django-treemenus
I'm curious to see if there is a better way to write out my menu.html which is my menu template. For each level that I add to my menu I have to manually add a level to my menu template.
Here is my menu.html (menu template). It works, but could it be written more efficiently?
{% load tree_menu_tags %}
{% ifequal menu_type "horizontal" %}
<ul><!-- Root -->
{% for menu_item in menu.root_item.children %}
<!-- Level 1-->
{% if menu_item.has_children %}
<li>{{ menu_item.caption }}
<ul>
{% for child in menu_item.children %}
<!-- Level 2-->
{% if child.has_children %}
<li>{{ child.caption }}
<ul>
{% for childchild in child.children %}
<!-- Level 3-->
{% if childchild.has_children %}
<li>{{ childchild.caption }}
<ul>
{% for childchildchild in childchild.children %}
{% show_menu_item childchildchild %}
{% endfor %}
</ul>
</li>
{% else %}
<li>{{ childchild.caption }}</li>
{% endif %}
<!-- End Level 3 -->
{% endfor %}
</ul>
</li>
{% else %}
<li>{{ child.caption }}</li>
{% endif %}
<!-- End Level 2 -->
{% endfor %}
</ul>
</li>
{% else %}
<li>{{ menu_item.caption }}</li>
{% endif %}
<!-- End Level 1 -->
{% endfor %}
</ul><!-- End Root -->
{% endifequal %}
I'd use a custom template tag for this kind of stuff: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
Here you can find some great examples: http://code.google.com/p/django-page-cms/source/browse/trunk/pages/templatetags/pages_tags.py?r=783