How to sort the output of this django template code in alphabetical order?
I have the following code in a django template:
{% for object in object_list %}
{% with game=object.game %}
<a href="{% url game_view game.id game.title|slugify %}" class="img_link alphabeticList">{{game.title}}</a>
{% endwith %}
{% endfor %}
It outputs all the games in the present category, for exa开发者_StackOverflowmple:
Armor Dude
Midnight Strike
Super Mafia Bros
Mission To Mars
Zed
game1 test 3
Examine the mystery of the crime in Apartment 13
Manor Freedom : The Patron's Chamber
Escape Journey
Outrun Summer Camp
Make your way through zombies hordes
Intelligent Guy
Run 3D : The Bathroom
Lady Gaga Escape Game
Insane Box
Will you do me A Small Favor?
Shanty Breakout
Cloudy Tumble Second Chaptert
Hotel Area Lock In
Escape From the Aircraft Hangar
RANDOM GAMES
Scene of the Caper: Gold Toy Scene of the Caper: ...
Escape Journey Escape Journey
Cloudy Tumble Second Chaptert Cloudy Tumble Second...
Midnight Strike Midnight Strike
BB EXCLUSIVES
Soldier Of Pain Soldier Of Pain
Walk The Stork Walk The Stork
How can I get the output to be sorted alphabetically and under the letter that each game title begins with, for example:
A
---
Apple
Alligator
B
---
Burger
Bungee
Bear
C
---
cat
and so on...
Please note, I can only use template tags and don't have access to any of the python code.
I hate throwing so much logic into the template. Typically I'd either have the list grouped in the view or have a custom tag to handle the processing for me. I really have no clue if something like this would work, but perhaps some combination of slicing, dicsorting, and regrouping can get you what you need?
If not then perhaps javascript might be your only way; although that's certainly not recommended.
{% regroup object_list|dictsort:"game.title" by game.title|upper|slice:"0" as grouped_list %}
{% for group in grouped_list %}
<p>{{ group.grouper }}</p>
{% for game in group.list %}
<a href="{% url game_view game.id game.title|slugify %}" class="img_link alphabeticList">{{ game.title }}</a>
{% endfor %}
{% endfor %}
精彩评论