user2224250
user2224250

Reputation: 251

Django: How to create temporary list in django template with out using template tags?

How to create temporary list in django template with out using template tags ?

if not possible to create temp list in django template, then how to use template tags for fixing the below scenario ?

Simple story: I have list called Info it contains bunch of duplicate values for instance ("hi","hello","hi","hello","hej","hey"). I want to display unique values in one div and all the values in another div in the same page using the same list Info

Please note: I have explained simple scenario here. So may be u think that use set method in python for preventing duplicate values. Not possible in my case,I have to send one object to django template

In one page there are two div's, it should show different information from the same object.

DIV 1:

{% for v in Info %}
       {{v}} // Show unique values ("hi","hello","hej","hey")            
{% endfor %}

DIV 2:

{% for v in Info %}
           {{v}} // Show all the values  ("hi","hello","hi","hello","hej","hey")             
 {% endfor %}

Please let me know your views

Upvotes: 1

Views: 1676

Answers (1)

srj
srj

Reputation: 10131

Using a custom filter:

@register.filter(name='unique')
def unique(value, arg):
    # put your complex unique logic here
    return set(value)

refer https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/#writing-custom-template-filters

use it as {% for v in Info|unique %} {{v}} {%endfor%} note that calling set will mess up the order of your list

Upvotes: 1

Related Questions