What is wrong with my logic in for loops - django templates - django

This is my home.html, problem is with the if block:
{% for menu in menus %}
<div class="dropdown show">
<a class="btn dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ menu.main_menu_item }}
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
{% for item in menu.items.all %}
{% for second_item in item.items_second_level.all %}
{% if item.items_second_level.all != None %}
<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">{{ item.sub_menu_item }}</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">{{second_item.sub_menu_item}}</a></li>
</ul>
</li>
{% else %}
<li><a class="dropdown-item" href="#">{{ item.sub_menu_item }}</a></li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
{% endfor %}
Namely, {% else %} part always "blanks". When I change condition to {% if item.items_second_level.all == None %}
again, only items which satisfies the if condition get showed (obviously this time the ones that were not shown before). it behaves like there is no else. Does anyone know what could be the issue here?

Related

Change categories url in OpenCart version 3.0.3.7

I would like to change the urls for the categories. On my page, there are 4 categories: Home,Shop,About,Contact. For example, when I hit "Home", I want it to load the home page. Is there anyway to do that on OpenCart?
Edit:
This is the meu.twig:
{% if categories %}
<div class="container">
<nav id="menu" class="navbar">
<div class="navbar-header"><span id="category" class="visible-xs">{{ text_category }}</span>
<button type="button" class="btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"><i class="fa fa-bars"></i></button>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
{% for category in categories %}
{% if category.children %}
<li class="dropdown">{{ category.name }}
<div class="dropdown-menu">
<div class="dropdown-inner"> {% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
<ul class="list-unstyled">
{% for child in children %}
<li>{{ child.name }}</li>
{% endfor %}
</ul>
{% endfor %}</div>
{{ text_all }} {{ category.name }} </div>
</li>
{% else %}
<li>{{ category.name }}</li>
{% endif %}
{% endfor %}
</ul>
</div>
</nav>
</div>
{% endif %}

Django - Pagination

I'm working on To-Do App.
I have two kinds of data,
Upcoming To-do Item and
Completed To-do Items.
So, I'm filtering data in views.py as upcoming items and completed items. I want to display them on the same page using pagination. I'm getting result too but if upcoming item completed in page-1 I was unable to visit page-2 because upcoming item for page-2 was empty, but I have items on completed items. I couldn't see that.
views.py
#login_required(login_url='login')
def home(request):
todo_form = TodoForm()
current = timezone.now()
todo_items_upcoming = Todo.objects.filter(user_id=request.user, completed=False).order_by('-date_created')
todo_items_completed = Todo.objects.filter(user_id=request.user, completed=True)
pagi1 = Paginator(todo_items_upcoming, 4)
pagi2 = Paginator(todo_items_completed, 4)
page_num = request.GET.get('page')
page_num2 = request.GET.get('page')
page_obj = pagi1.get_page(page_num)
page_obj2 = pagi2.get_page(page_num2)
if request.method == "POST":
todo_form = TodoForm(request.POST)
if todo_form.is_valid():
data = todo_form.cleaned_data.get('title')
obj = Todo.objects.create(date_created=current, title=data, user_id=request.user)
context = {'todo_form':todo_form, 'page_obj':page_obj, 'page_obj2':page_obj2}
return render(request, 'todo/main.html', context)
main.html
{% extends 'todo/index.html' %}
{% load crispy_forms_tags %}
{% block content %}
<nav aria-label="...">
<ul class="pagination justify-content-center">
<!-- pagination - previous pages -->
{% if page_obj.has_previous or page_obj2.has_previous %}
<li class="page-item">
{% if page_obj.has_previous %}
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" tabindex="-1">«</a>
{% else %}
<a class="page-link" href="?page={{ page_obj2.previous_page_number }}" tabindex="-1">«</a>
{% endif %}
</li>
{% endif %}
<!-- pagination - current page -->
<li class="page-item active">
<a class="page-link" href="?page={{ page_obj.number }}">{{ page_obj.number }}</a>
</li>
<!-- pagination - Next pages -->
{% if page_obj.has_next or page_obj2.has_next %}
<li class="page-item">
{% if page_obj.has_next %}
<a class="page-link" href="?page={{ page_obj.next_page_number }}">»</a>
{% else %}
<a class="page-link" href="?page={{ page_obj2.next_page_number }}">»</a>
{% endif %}
</li>
{% endif %}
</ul>
</nav>
<div class="center-column">
<h2 class="heading" style="text-align: center;">Hello {{ request.user.username }}, Here's your ToDo List</h2>
<form action="" method="POST" id='form-create'>
{% csrf_token %}
<div style="text-align: center;">
{{ todo_form.title }}
<button type="submit" class="form-control btn btn-primary mb-3 mr-sm-2">Add Items</button>
</div>
</form>
<h4 class="heading" style="text-align: center;">Upcoming ToDo Items</h4><br>
<div class="row">
<div class="col" id="upcomItem">
<ul class="list-group" id="upcomingItems">
{% for i in page_obj %}
<li class="list-group-item list-group-item-primary mb-2" id="upcomingItem">{{ i.title }}
<div class="float-right">
<button type="submit" class="btn-sm btn-danger ml-2 mt-2 mr-2 mb-1">Delete</button>
</div>
<div class="float-right">
<button type="submit" class="btn-sm btn-success ml-2 mt-2 mr-2 mb-1" id="update_btn">Update</button>
</div>
<div class="float-right">
<button type="submit" class="btn-sm btn-dark ml-2 mt-2 mr-2 mb-1" id="completed_btn">Completed</button>
</div>
</li>
{% endfor %}
</ul>
</div>
</div>
<hr style="border-top: 1px solid #999; ">
<div class="row">
<div class="col">
<h4 class="heading" style="text-align: center;">Completed ToDo Items</h4>
<ul class="list-group" id='compItems'>
{% for i in page_obj2 %}<hr>
<li class="list-group-item list-group-item-primary mb-2" id="upcomingItem">{{ i.title }}
<div class="float-right">
<button type="submit" class="btn-sm btn-danger ml-2 mt-2 mr-2 mb-1">Delete</button>
</div>
<div class="float-right">
<button type="submit" class="btn-sm btn-success ml-2 mt-2 mr-2 mb-1" >Update</button>
</div>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endblock %}
At first I have used or in template tags in if condition I got this error. Error Message occurs when I try to add new to-do item
I tried and operator in template tags I didn;'t even get page-2 link eventhough I have upcoming items and no extra completed items.
Use or operator as #Willem pointed. And you active link href must be {{ page_obj.number }} instead {{ page_obj.page_number }}:
<li class="page-item active">
<a class="page-link" href="?page={{ page_obj.number }}">{{ page_obj.number }}</a>
</li>
As you can see your error EmptyPage raised in your template at line 18
Your conditions should be a disjunction of the conditions of the paginators, so or instead of and:
{% if page_obj.has_previous or page_obj2.has_previous %}
…
{% endif %}
the same for .has_next. The previous and next page then should be handled by checking which page object has a next page, so:
{% if page_obj.has_next or page_obj2.has_next %}
<li class="page-item">
{% if page_obj.has_next %}
<a class="page-link" href="?page={{ page_obj.next_page_number }}">»</a>
{% else %}
<a class="page-link" href="?page={{ page_obj2.next_page_number }}">»</a>
{% endif %}
</li>
{% endif %}
That being said, I'm not sure this provides the best user experience. Perhaps it is better to make two separate paginations (so work with ?pagedone=…&pagetodo=…).

TranslatablePage matching query does not exist wagtail custom menu issue

when i am creating wagatil menu translatable page does not exist error display
{% load wagtailimages_tags cms_tags %}
{% get_menu "main" None request.user.is_authenticated as navigation %}
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto mr-5">
{% for item in navigation %}
{% get_menu item.slug item.page request.user.is_authenticated as submenu %}
<li class="{% if submenu %}dropdown {% endif %}p-2">
<div class="dropdown show">
<a href="{{ item.url }}"
{% if submenu %} class="menuitem dropdown-toggle {% if item.icon %}menuicon{% endif %}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
{% else %} data-toggle="tooltip" title="{{ item.title }}" class="menuitem"
{% endif %}
>
{% if item.icon %}
{% image item.icon fill-30x30 class="image-menu" %}
{% else %}
{{ item.title }}
{% endif %}
</a>
{% if submenu %}
<div class="dropdown-menu">
{% for subitem in submenu %}
<a href="{{ subitem.url }}" class="dropdown-item menuitem p-2 {% if subitem.icon %}menuicon{% endif %}">
{% if subitem.icon %}
{% image subitem.icon fill-30x30 class="image-menu" %}
{% else %}
{{ subitem.title }}
{% endif %}
</a>
{% endfor %}
</div>
{% endif %}
</div>
</li>
{% endfor %}
</ul>
</div>

That page contains no results Django ListView

I have the problem in Django 1.9
I have this problem when I go to the last result pagination
views.py
class UserList(ListView):
model = User
template_name = 'account/users.html'
paginate_by = 1
users.html
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
previous
{% endif %}
<span class="page-current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
next
{% endif %}
</span>
I've encountered this as well and the problem is that Paginator.page_range is 1-based.
https://docs.djangoproject.com/en/1.9/topics/pagination/#django.core.paginator.Paginator.page_range
I've used the following solution: (I'm using Foundation 6)
{% if is_paginated %}
<ul class="pagination text-center" role="navigation" aria-label="Pagination">
{% if page_obj.has_previous %}
<li class="pagination-previous">
<a href="?page={{ page_obj.previous_page_number }}">
<span>Previous</span>
</a>
</li>
{% else %}
<li class="pagination-previous disabled">
<span>Previous</span>
</li>
{% endif %}
{% for page in paginator.page_range %}
{% if paginator.num_pages != page %}
<li class="{% if page == page_obj.number %}active{% endif %}">
{{ page }}
</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next and paginator.num_pages != page_obj.next_page_number %}
<li class="pagination-next">
<a href="?page={{ page_obj.next_page_number }}">
<span>Next</span>
</a>
</li>
{% else %}
<li class="pagination-next disabled">
<span>Next</span>
</li>
{% endif %}
</ul>
{% endif %}
Hope this helps! I initially based my code from https://ana-balica.github.io/2015/01/29/pagination-in-django/

Django pagination - example from documentation. How to display with all sites number?

This is example from documentation Django:
def listing(request):
contact_list = Contacts.objects.all()
paginator = Paginator(contact_list, 25) # Show 25 contacts per page
page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
return render_to_response('list.html', {"contacts": contacts})
template:
{% for contact in contacts %}
{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br />
...
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if contacts.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
</span>
{% if contacts.has_next %}
next
{% endif %}
</span>
</div>
This display for example:
Page 2 of 3. next
How to display it in this way:
previous 1 <b>2</b> 3 Next
Current page with html <b> mark.
?
You can try this:
{% for num in contacts.paginator.page_range %}
{% ifequal num contacts.number %}
<span class="current"><b>{{ num }}</b></span>
{% else %}
{{ num }}
{% endifequal %}
{% endfor %}
Below example is actually for class based views but would work fine.
CSS is Twitter Bootstrap V3.0.0.
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li>«</li>
{% else %}
<li class="disabled">«</li>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% ifequal num page_obj.number %}
<li class="active">{{ num }}<span class="sr-only">(current)</span></li>
{% else %}
<li>{{ num }}</li>
{% endifequal %}
{% endfor %}
{% if page_obj.has_next %}
<li>»</li>
{% else %}
<li class="disabled">»</li>
{% endif %}
</ul>
{% endif %}
This is mine.
{% if page.paginator.num_pages > 1 %}
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{% if page.paginator.num_pages != 1 %}
<li class="page-item"><a class="page-link" href="?page=1">First</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">First</a></li>
{% endif %}
{% if page.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ page.previous_page_number }}">«</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">«</a></li>
{% endif %}
{% for i in page.paginator.page_range %}
{% if page.number == i %}
<li class="page-item active"><a class="page-link" href="#">{{ i }} <span class="sr-only">(current)</span></a></li>
{% elif page.number > i|add:"-5" and page.number < i|add:"+5"%}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page.next_page_number }}">»</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">»</a></li>
{% endif %}
{% if page.paginator.num_pages != page.number %}
<li class="page-item"><a class="page-link" href="?page={{ page.paginator.num_pages }}">Last</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">Last</a></li>
{% endif %}
</ul>
</nav>
{% endif %}
Looks like
If someone needs Bootstap 4 Alpha 5 version of code.
Also I did two changes:
I do not like duplicates of pages, so I changed in my case /profiles/?page=1 to simple /profiles/.
I have lots of pages, so I do not show all pages. I show just: The 1st, the last, current and +-3 pages from the current one, and every 10th page.
If you need just all pages with Bootstrap 4 without my modifications, just remove unnecessary parts of code (everything is commented).
{% if is_paginated %}
<nav aria-label="Page navigation">
<ul class="pagination">
<!-- << PREVIOUS PART -->
<!-- << Disable 'Previous' page button if you are at the 1st page -->
{% if not page_obj.has_previous %}
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-label="Previous">
<!-- << If you are at the 2nd page,
'Previous' page button will get '/profiles/' url instead of '/profiles/?page=1' -->
{% elif page_obj.previous_page_number == 1 %}
<li class="page-item">
<a class="page-link" href="{{ profiles_1st_page_url }}" aria-label="Previous">
{% else %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
{% endif %}
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
<!-- PAGES PART -->
{% for num in page_obj.paginator.page_range %}
<!-- Active page -->
{% if num == page_obj.number %}
<li class="page-item active">
<a class="page-link" href="#">{{ num }}<span class="sr-only">(current)</span></a>
</li>
{% else %}
<li class="page-item">
<!-- For the 1st page we do not use ?page=1 -->
{% if num == 1 %}
<a class="page-link" href="{{ profiles_1st_page_url }}">{{ num }}</a>
{% else %}
<!-- Show current page and +-3 pages -->
{% if num|add:"-3" <= page_obj.number and page_obj.number <= num|add:"3" %}
<a class="page-link" href="?page={{ num }}">{{ num }}</a>
<!-- Shows every 10th page and the last page -->
{% elif num|divisibleby:"10" or num == page_obj.paginator.num_pages %}
<a class="page-link" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endif %}
</li>
{% endif %}
{% endfor %}
<!-- >> NEXT PART -->
{% if not page_obj.has_next %}
<!-- << Disable 'Next' page button if you are at the last page -->
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-label="Next">
{% else %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
{% endif %}
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
</ul>
</nav>
{% endif %}
And it looks like: