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

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>

Related

Unable to add different block tag to my template

I am trying to add different block tags inside a for loop but it raise an error
Did you forget to register or load this tag?
But I register it
{% for todo in todo_list %}
{% if todo.complete %}{% else %}
{{todo.text|capfirst|truncatechars:150}} </a> <br>
<small class="text-muted">{{todo.content|capfirst}}{% empty %} {% endif %} </small> <hr>
{% endif %} {% endfor %}
Thanks
Looking into your problem, I think you need to try this:
{% for todo in todo_list %}
{% if todo.complete %}
{% else %}
{{todo.text|capfirst|truncatechars:150}} </a> <br>
{% if todo.content %}
<small class="text-muted">{{todo.content|capfirst}} </small> <hr>
{% else %}
//do something
{% endif %}
{% endif %}
{% endfor %}

'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.

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

Listing Tags of a Post in Django CMS and Aldryn NewsBlog

I am trying to figure out how to display tags belonging to an article created within Aldryn NewsBlog plugin. Unfortunately, I cannot find any documentation on how do it.
I was able to display categories using the following code.
<span style="margin: 0; display: block">
<h4 style="display:inline-flex">Categories:</h4>
{% for category in article.categories.all %}
{{ category.name }} {% if not forloop.last %}, {% endif %}
{% endfor %}
</span>
For tags, I am using this code:
<span style="margin: 0; padding-bottom: 0; display: block">
<h4 style="display:inline-flex">Tags:</h4>
{% for tag in article.tag %}
{{ tag.name }} {% if not forloop.last %}, {% endif %}
{% endfor %}
</span>
What am I doing wrong? Could anyone tell me how to display tags?
this is the official tags template of aldryn-newsblog, it worked for me:
{% load i18n apphooks_config_tags %}
<div class="aldryn aldryn-newsblog aldryn-newsblog-tags">
<ul class="list-unstyled">
<li{% if not newsblog_tag %} class="active"{% endif %}>
{% trans "All" %}
</li>
{% for tag in tags %}
<li{% if newsblog_tag.id == tag.id %} class="active"{% endif %}>
<a href="{% namespace_url "article-list-by-tag" tag.slug namespace=instance.app_config.namespace default='' %}">
{{ tag.name }}
<span class="badge">{{ tag.article_count }}</span>
</a>
</li>
{% endfor %}
</ul>
https://github.com/aldryn/aldryn-newsblog/blob/master/aldryn_newsblog/boilerplates/bootstrap3/templates/aldryn_newsblog/plugins/tags.html
you're right, that is what you're looking for, with article.tags.all:
{% if article.tags.exists %}
<ul style="margin-left: 0">
{% for tag in article.tags.all %}
<li class="tags">{{ tag.name }}</li>
{% if not forloop.last %}<span class="separator tags-separator">|</span> {% endif %}
{% endfor %}
</ul>
{% 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 %}