I currently have a data model set up where each School object has a name. On the primary page of the my website I am outputting all the School objects sorted alphabetically by name. I was hoping there would be a way for Django to create index titles for the objects? By this I mean categorize each School object by the first letter of it’s name. This would prove very useful to me as I can then provide section titles for the potentially long list of schools. Ideally, instead of returning all school objects to the template, I’d want to manipulate / sort the objects in a way such that they were categorized into a dictionary of arrays, where the keys of the main dictionary are the letters in the alphabet (excluding those for which there are no school names starting with that letter), and the values are arrays of schools whose name starts with the first letter of the corresponding key. This would make it very easy to loop over in the django templating system to accomplish what i want to do with section headers. Any help would be greatly appreciated. Is this native to Django? Or am I going to have to employ some python logic?
Share
you can’t get the database to group them, but it’s a fairly easy piece of code: