Django template using variable in two blocks - django

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

Related

'else', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

{% if movies.has_other_pages %}
<ul class="pagination">
{% if movies.has_previous %}
<li class="pagination-item">
Previous
</li>
{% else %}
<li class="pagination-item">
<a>Previous</a>
</li>
{% endif %}
{% for i in movies.paginator.page_range %}
{% if movies.number == i %}
<li class="pagination-item current-page"><a>{{i}}</a></li>
{% else %}
<li class="pagination-item">{{i}}</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
I'm trying to use paginator in django but it seems like something is wrong but I can't seem to figure it out.

How to use django paginator page range for displaying the first 10 numbers not all?

I want to change the blow script to show just the first 10 page numbers. Now it shows all page numbers. Could you please tell me how to change it? I tried several ways but it didn't work. Any help will be appreciated!
{% if forloop.counter|divisibleby:"3" or forloop.last %}
</div>
{% endif %}
{% endfor %}
{% if is_paginated %}
<div class="row">
<ul class="pagination pagination-md ">
{% if page_obj.has_previous %}
<li>«</li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }}</span></li>
{% else %}
<li><a href="?page={{ i }}{% if currentCategory %}&category={{ currentCategory }}
{% endif %}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
</div>
{% endif %}
</div>

Pagination on results doesn't work 'afte the first page' other papes repeats the first

I have an issue with pagination in Django search.html page. After search result, the results on first page of the table is repeated on the next pages continously.
For reference, this is the bit of the template that displays results:
{% for result in page_obj.object_list %}
<p>
{{ result.object.title }}
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page_obj.has_previous or page_obj.has_next %}
<div>
{% if page_obj.has_previous %}{% endif %}« Previous{% if page_obj.has_previous %}{% endif %}
|
{% if page_obj.has_next %}{% endif %}Next »{% if page_obj.has_next %}{% endif %}
</div>
{% endif %}

Custom menu in Mezzanine

I have following mezzanine tree.html
{% if page_branch_in_menu %}
<ul class="nav nav-list navlist-menu-level-{{ branch_level }}">
{% for page in page_branch %}
{% if page.in_menu %}
{% if page.is_current_or_ascendant or not page.is_primary %}
<li class="
{% if page.is_current %} active{% endif %}
{% if page.is_current_or_ascendant %} active-branch{% endif %}
" id="tree-menu-{{ page.html_id }}">
{{ page.title }}
{% if page.has_children_in_menu %}{% page_menu page %}{% endif %}
</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
{% endif %}
In the base.html I have following block:
<div class="col-md-2 left">
{% block left_panel %}
<div class="panel panel-default tree">{% page_menu "pages/menus/tree.html" %}</div>
{% endblock %}
</div>
The problem is that on some pages where the menu is empty the css styles are applied to the divs with empty menu lists and users can observe empty containers. In html it looks like:
<div class="col-md-2 left">
<div class="panel panel-default tree">
<ul class="nav nav-list navlist-menu-level-0"></ul>
</div>
</div>
I can hide the child ul with something like .nav:empty { display:none;} but the parent will still be visible. Here is the discussion about similar question: :empty selector for parent element
Is it possible to solve this problem with Mezzanine template tags?
{% if page_branch %} doesn't help because it's full of pages which are all not in_menu.
So better filter them before context.
menu_pages = page_branch.filter(in_menu=True)
Also you should put if block on top of div.tree
{% if menu_pages %}
<div class="panel panel-default tree">{% page_menu "pages/menus/tree.html" %}</div>
{% endif %}
Another way is to write custom filter
{% with menu_pages=page_branch|filter_in_menu %}
{% if menu_pages %}
...
{% endif %}
{% endif %}
But there is no way to apply extra filter to queryset with built-in syntax or Mezzanine tags.
You could wrap the code with an if tag.
{% if page_branch %}
<ul>
{% for page in page_branch %}
{% if page.in_menu %}
{% if page.is_current_or_ascendant or not page.is_primary %}
<li>
{% if not page.is_primary %}
{{ page.title }}
{% endif %}
{% page_menu page %}
</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
{% endif %}

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