Reputation: 2417
I have a Django Filter that that has 4 choices. I would like to order these choices on the template in the order that I have written them in the filter.
filter.py:
RarityChoices = {
('common', 'Common'),
('uncommon', 'Uncommon'),
('rare', 'Rare'),
('mythic', 'Mythic')
}
rarity = ChoiceFilter(field_name='rarity', empty_label='Any', choices=sorted(RarityChoices), widget=RadioSelect())
template.html:
<div>
<label class="mb-1" for="id_type">Rarity:</label>
{{ card_filter.form.rarity}}
</div>
Screenshot:
If I change my filter to:
rarity = ChoiceFilter(field_name='rarity', empty_label='Any', choices=RarityChoices, widget=RadioSelect())
The filter looks like this on the template:
Neither are in the order that I want or in the order listed in the filter.py
file.
Upvotes: 0
Views: 262
Reputation: 114098
RarityChoices = {
('common', 'Common'),
('uncommon', 'Uncommon'),
('rare', 'Rare'),
('mythic', 'Mythic')
}
is a set ... sets are unordered ... change it to a list or a tuple
RarityChoices = [ # [ is for list
('common', 'Common'),
('uncommon', 'Uncommon'),
('rare', 'Rare'),
('mythic', 'Mythic')
]
Upvotes: 5