debug is answering me that there is any if without endif tag.
{% for j in content.numpaginas %}
{% if j == content.actpag %}
<li class="active blue-grey"></li>
{% else %}
<li class="waves-effect"></li>
{% endif %}
{% endfor }
replace = with ==
{% if j == content.actpag %}
Django also have {% ifequal foo bar %} tag.
See here.
Related
I have a list of objects that I would like to display in an ul/li. For that I do the code below:
<ul id="myUL">
{% for l in lpps %}
<li id="lpps">{{ l.codeACL }} {{ l.libelle }}</li>
{% endfor %}
</ul>
The problem is that in my view, I ask to display only 15 objects per page.
But I want to ignore this and display all the objects on all the pages.
Is there something like for l in lpps.page(all)...?
django have forloop.counter ,
you can use that like
<ul id="myUL">
{% for l in lpps %}
{% if forloop.counter == 15 %}{% break %}{% endif %}
<li id="lpps">{{ l.codeACL }} {{ l.libelle }}</li>
{% endfor %}
</ul>
I would like to make a pagination with Bootstrap : a new page every 10 new field in data.
file.html
{% for d in data %}
{% if forloop.first %}
<ul class="pagination">
{% endif %}
{% if (forloop.counter % 10) == 0 %}
<li>{{ forloop.counter % 10 }}</li>
{% endif %}
{% if forloop.last %}
</ul>
{% endif %}
{% endfor %}
output I would like that => Bootstrap pagination
But Django give me an error for this :
{% if (forloop.counter % 10) == 0 %}
TemplateSyntaxError :/
I don't know how to do except create my own filter or add a filter, but i would like to know first if i can do in the template first.
PS: I use Django 1.5 and I can't upgrade it.
Edit:
Finally I use this condition:
{% if forloop.counter|divisibleby:'10' and forloop.counter|divisibleby:'5' and forloop.counter|divisibleby:'2' %}
Like that I know when I have a 10 multiple.
The modulus (%) operator is not available in django templates. However, you can use the divisibleby (https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#divisibleby) template filter, something like
{% if forloop.counter|divisibleby:"2" %}
Use the paginator, your QuerySet are not evaluated for the hole table, just the number you need to build the page, and it offers properties that you can use in the template like (page_range, next_page_number, has_next, etc.)
here is the code withe BootStrap 2 and django.core.paginator:
<div class="pagination pagination-centered">
<ul>
{% if MYDATAENTIRES.has_previous %}
<li>
{% trans "Précédent" %}
</li>
{% endif %}
{% for i in MYDATAENTIRES.paginator.page_range %}
<li {% ifequal MYDATAENTIRES.number i %} {{ 'class="disabled"' }} {% endifequal %}>
<a href="?page={{ i }}">
{{ i }}
</a>
</li>
{% endfor %}
{% if MYDATAENTIRES.has_next %}
<li>
{% trans "Suivant" %}
</li>
{% endif %}
</ul>
</div>
I have simple issue:
I have this {{ objects.paginator.num_pages }} in template, which gives me the total number of pages that contain items.
now i want to show those page numbers like this
1 | 2 | 3 | 4 | 5
to achieve this, i need to make forloop till the num_pages. like for i to num_pages.
how is it possible in django template? i am reading some snippets, but they are a bit difficult to understand for me.
Formatted with twitter bootstrap and with links:
<ul class="pagination nav navbar-nav">
{% if objects.has_previous %}
<li>Prev</li>
{% endif %}
{% for page in objects.paginator.page_range %}
<li class="{% if objects.number == page %}active{% endif %}">{{ page }}</li>
{% endfor %}
{% if objects.has_next %}
<li> Next</li>
{% endif %}
</ul>
You can use page_range
{% for page in objects.paginator.page_range %}
{% if forloop.counter != 1 %} | {% endif %}
{{ page }}
{% endfor %}
Maybe a stupid question but how do I remove a sublist from list in django template
I have something like this:
{% for j in sub_com|slice:"1" %}
{% for k in j|slice:"3" %}
<li> {{k}} </li>
{% endfor %}
{# remove sublist from list here #}
{% endfor %}
You can probably use templateContext.pop here.
{% for j in sub_com|slice:"1" %}
{% for k in j|slice:"3" %}
<li> {{k}} </li>
{% endfor %}
{{ j.pop.0 }}
{% endfor %}
I have the following code in my template:
{% for f in friendslist %}
{% if forloop.first %}
// display something
{% endif %}
// display stuff
{% if forloop.last %}
// display something
{% endif %}
{% endfor %}
It works as expected when there is more than one item in the friendslist. But if there is just 1 item, then the content inside the forloop.last
conditional does not display.
I guess this is because the loop in that case is the first, but I mean it's also the last right? So why dosn't both contents inside first and last
conditional show?
In my code they both execute if there is only one element in friendslist. Here is a test you can run in the shell where maybe you can figure out what isn't working:
$ ./manage.py shell
>>> from django import template
>>> t = template.Template("""{% for f in friendslist %}
{% if forloop.first %}
First of the loop
{% endif %}
{{ f }}
{% if forloop.last %}
Last of the loop
{% endif %}
{% endfor %}""")
>>> c = template.Context({'friendslist' : ['one element',]})
>>> t.render(c)
First of the loop
one element
Last of the loop
couldn't you just use an "or" tag like {% if forloop.last or friendlist.count == 1 %}
{% for f in friendslist %}
{% if forloop.first %}
// display something
{% endif %}
// display stuff
{% if forloop.last or friendlist.count == 1 %}
// display something
{% endif %}
{% endfor %}
You can also use ''not'' in template to solve this. It works even there is just an item in the list.
{% for friend in friendslist %}
{% if not forloop.last %}
// display something
{% endif %}
{% if forloop.last %}
// display something
{% endif %}
{% endfor %}