How turn {{ fieldset.fields }} in Django template into a string? - python

What does Django return with using {{ fieldset.fields }}? How can I make it a string?
In my template, I have this:
{% for fieldset in adminform %}
<li> {{ fieldset.fields }} </li>
{% if "nanoadded" in fieldset.fields %}
<li> nanoadded is here </li>
{% else %}
<li> nanoadded is NOT here </li>
{% endif %}
{% endfor %}
Here is what is returned:
[('arri', 'aconcentration', 'acat', 'anotes', 'agtlt', 'id'), ('nanoadded', 'response', 'select_charc')]
nanoadded is NOT here
So I am assuming that the fieldset.fields is not returning a string (even though it looks like a string). How can a make Django see the contents of fieldset.fields as a string? Thanks for you assistance!

It looks like the fields property returns a list that contains two tuples, so you might want to run it through a for loop to check each tuple for the membership of the string 'nanoadded'
Perhaps like this:
{% for fieldset in adminform %}
{% for field in fieldset.fields %}
<li> {{ field }} </li>
{% if "nanoadded" in field %}
<li> nanoadded is here </li>
{% else %}
<li> nanoadded is NOT here </li>
{% endif %}
{% endfor %}
{% endfor %}

Related

Django pagination - for loop in all pages

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>

Django Template: Exclude request.user from displaying user list

I have a simple problem. Below html code return list of all users whose liked blog. How can i exclude request user from list of them ?
<div class="collapse" id="likes">
<ul>
{% for like in instance.likes.all %}
<li>
{{like}}
</li>
{% endfor %}
</ul>
</div>
I tried as : {{like |request.user}} or something like this ,but no result. Thanks in advance
The easiest way to do this is probably using an {% if ... %} to filter out that particular user:
<div class="collapse" id="likes">
<ul>
{% for like in instance.likes.all %}
{% if like != request.user %}
<li>
{{like}}
</li>
{% endif %}
{% endfor %}
</ul>
</div>

Is it possible to pass a dictionary of objects from a view to a template

I am wondering if we can pass a dictionary of objects to a template and loop it in a template.
This is my structure:
{
'Communication': [
[EchoCase: EchoCase object, EchoDescription: EchoDescription object],
[EchoCase: EchoCase object, EchoDescription: EchoDescription object]
],
'escalations': [[EchoCase: EchoCase object, EchoDescription: EchoDescription object],
[EchoCase: EchoCase object, EchoDescription: EchoDescription object]]
}
It is basically a dictionary where for each key, the value is a list of list.
I would like to know if there is a way to loop on this within a django template or if this kind of structure is too complicated,
{% for key, value_list in data %}
<ul>
<li> {{ key }}
<ul>
{% for value in value_list %}
<li>{{ value }}</li>
{% endfor %}
</ul>
</li>
</ul>
{% endfor %}
But it looks like Echo Case and Echo Description are related so you might just want to use related fields and do {{ echocase }} {{ echocase.description }} or something similar
I managed to make it worked using Paul's structure
{% for category, liste in template_dictionnary.items %}
<ul>
<li> {{ category }} </li>
<li> {{ liste}} </li>
{% for val in liste %}
<li>Val: {{ val.0.comment }}</li> <!-- val.0 is an EchoCase, val.1 is an EchoDescription -->
<li>Val: {{ val.1.field }}</li>
{% endfor %}
</li>
</ul>
{% endfor %}
Thank you Paul

Error in the template with Django : can I make arithmetic in if statement

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>

Django template remove sub list

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

Categories