Home Django- template loop but exclude repeats
Reply: 0

Django- template loop but exclude repeats

user5418 Published in September 19, 2018, 9:09 am

Say I have this data and I'm passing to my template:

apps = [
 {'category': 'one', 'item': 'blah'},
 {'category': 'one', 'item': 'blah'},
 {'category': 'two', 'item': 'blah'},
 {'category': 'two', 'item': 'blah'},
 {'category': 'three', 'item': 'blah'}

Then in my template I want to add one div withe the id 'category' but not repeat any divs if they've been added before with the id. So something similar to:

{% for app in apps %}
   <div id="{{app.category}}"></div>
{% endfor %}

But I only want these to be rendered:

<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
share|improve this question
  • 1
    I think it will be much easier to adjust the data in your view before passing to the template. – John Gordon Jan 12 at 3:16

1 Answer 1

active oldest votes
up vote 2 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.44385 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO