Best way to do an A-Z list in Django?
I've been hunting around and Googling for examples, but I can't see an obvious best way to do an A-Z list of entries in Django.
I have around 2,000 placenames in a db. Rather than display them all on the same page, I开发者_C百科'd like to have tabs or hyperlinks and an A-Z list of names.
Something like this A-Z list. Bonus points if it's possible to create a similar page which allows users to page through by a db field, rather than A-Z.
Surely I don't need to roll my own code for this in Django - it must be a common problem. Can anyone provide any good examples, or even a Django app to make this straightforward?
You just need to filter your queryset according to what the user has selected:
Case insensitive:
Entry.objects.filter(field__istartswith='a')
Case sensitive:
Entry.objects.filter(field__startswith='A')
Alphabetical pagination might also be useful to you -- see something like https://djangosnippets.org/snippets/1364/
精彩评论