Django, I have added endif but it still shows an error? - django

my code
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li> Post </li>
<li> Groups </li>
<li> Create Group </li>
<li> Log Out </li>
{% else %}
<li> Groups </li>
<li> Log In </li>
<li> Sign Up </li>
{& endif %}
error shown is
Unclosed tag on line 30: 'if'. Looking for one of: endif.

You have {& endif %}
The correct syntax is {% endif %}

Related

about accessing li contents without ul contents inside of that li

im trying to access the text content that is "education" in the given code stand it is written in a li element. But not the text content that are written inside the ul of that li element that is "science" and other li elements inside science. Basically, li has uls and i just want to access li content and not ul's.
<ul class="dropdown-items">
<li class="li-items">Education
<ul class="dropdown-items">
<li data-parent="Education" class="li-items">Science
<ul class="dropdown-items">
<li data-parent="Science" class="li-items">Physics</li>
<li data-parent="Science" class="li-items">Chemistry</li>
<li data-parent="Science" class="li-items">Biology</li>
</ul>
</li>
</ul>
</li>
<li class="li-items">Entertainment</li>
<li class="li-items">Spiritual</li>
</ul>
that was my html
that come out dyanamically because of following code in template:
<ul class="dropdown-items">
{% for cat in cats %}
{% if not cat.children.all %}
<li {% if cat.parent %} data-parent="{{cat.parent.name}}" {% endif %} class="li-items">{{cat.name}}
{%else%}
<li {% if cat.parent %} data-parent="{{cat.parent.name}}" {% endif %} class="li-items">{{cat.name}}
{% include "blog/category.html" with cats=cat.children.all %}
</li>
</li>
{% endif %}
{% endfor %}
</ul>

Django why the pagination is not working?

Sorry because of the dumb question. I am reading a tutorial from book called build django 2 web application. and I get to the pagination topic but I can't get why it does not working even when I am copy-pasting carefully.
{% if is_paginated %}
<nav>
<ul class="pagination">
<li class="page-item">
First
</li>
{% if page_obj.has_previous %}
<li class="page-item">
{{page_obj.previous_page_number}}
</li>
{% endif %}
<li class="page-item active">
{{page_obj.number}}
</li>
{% if page_obj.has_next %}
<li class="page-item">
{{page_obj.next_page_number}}
</li>
{% endif %}
<li class="page-item">
Last
</li>
</ul>
</nav>
{% endif %}
#View
class MovieListView(ListView):
model = Movie
template_name = 'movie_list.html'
You haven't set the paginated_by attribute in the view class, so the contents won't be paginated.
class MovieListView(ListView):
model = Movie
template_name = 'movie_list.html'
paginate_by = 5

Django TemplateError when using if...else block inside for loop

I have the following as part of my django template:
<nav>
<ul class="pagination">
{% if page.has_previous %}
<li>
<a href="{% url 'main:stream_detail' stream_id=stream.id %}?p={{page.next_page_number}}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
{% else %}
<li class="disabled" aria-label="previous"><span aria-hidden="true">«</span></li>
{% endif %}
{% for i in paginator.page_range %}
(% if i == page.number %}
<li class="active">{{i}} <span class="sr-only">(current)</span></li>
{% else %}
<li>{{i}}</li>
{% endif %}
{% endfor %}
{% if page.has_next %}
<li>
<a href="{% url 'main:stream_detail' stream_id=stream.id %}?p={{page.next_page_number}}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
{% else %}
<li class="disabled" aria-label="next"><span aria-hidden="true">»</span></li>
{% endif %}
</ul>
</nav>
And I'm getting the following exception:
Invalid block tag on line 26: 'else', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Line 26 corresponds to the {% else %} clause inside the {% for %} loop. As fas as I can tell, this else clause correctly matches up with the if statement, but the template parser seems to expect a clause to match the for loop there.
You have a syntax error with the if tag in the for loop:
(% if
...should be
{% if
You used '(' instead of '{' on line (% if i == page.number %}

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/

Determining first and last element for each level in django-mptt

I am using django mptt to display navigational menu.
{% load mptt_tags %}
<ul class="nav_menu">
{% recursetree nav_link.get_descendants %}
{% if node.is_shown %}
<li>
{{ node.title }}
{% if not node.is_leaf_node %}
<ul class="nav_menu">
{{ children }}
</ul>
{% endif %}
</li>
{% endif %}
{% endrecursetree %}
</ul>
Is there a way to mark each first child of each nav_menu with a class first-child and to mark each last child of each nav_menu with a class last-child?
For example:
<ul class="nav_menu">
<li class="first-child">
Node 1
<ul class="nav_menu">
<li class="first-child last-child">
Node 1.1
</li>
</ul>
</li>
<li>
Node 2
<ul class="nav_menu">
<li class="first-child">
Node 2.1
</li>
<li class="last-child">
Node 2.2
</li>
</ul>
</li>
<li class="last-child">
Node 3
</li>
</ul>
A node can know whether or not it is the first or last at its level by querying get_previous_sibling and get_next_sibling.
<a class="{% if not node.get_previous_sibling %}first_child {% endif %}{% if not node.get_next_sibling %}last_child{% endif %} href="{{ node.url }}">{{ node.title }}</a>
These calls should work on the node cache, so won't hit the database. However, CSS already has pseudo-selectors for first-child and last-child, so it might be better to just do any styling using those rather than with explicit classes unless you're targeting older browsers.
If not all items in the database should be shown depending on a calculated value for each node, then first item and last item in the database don't necessary match first item and last item in the list. In this case, the first item and last item can be marked by javascript/jquery:
$(document).ready(function() {
$('.nav_menu li:first-child').addClass("first-child");
$('.nav_menu li:last-child').addClass("last-child");
});