[英]Paginated based on the alphabet (A-Z)
I have a simple contacts application where I want to paginate the contact list based on first name alphabet. 我有一个简单的联系人应用程序,我想根据名字字母对联系人列表进行分页。
Here is my views.py 这是我的views.py
def contacts(request):
contact_list = Contact.objects.filter(user=request.user).order_by('first_name')
return render(request, 'contact/contact.html', {'contact_list': contact_list})
Here is my template 这是我的模板
<ul>
{% for contact in contact_list %}
<li><a href="/contacts/{{ contact.id }}/">{{ contact.first_name }}</a></li>
{% endfor %}
</ul>
Is there a default way in Django that does this? Django中是否有执行此操作的默认方法? There is Django pagination but I think that only splits data across pages.
有Django分页功能,但我认为只能在页面之间拆分数据。 What would be the easiest way to do so?
这样做最简单的方法是什么?
It seems a letter-wise paginator does not exist by default. 似乎默认情况下不存在按字母排序的分页器。 Pages like https://djangosnippets.org/snippets/1364/ <<< this one show hand-made implementations.
像https://djangosnippets.org/snippets/1364/之类的页面<<<这是手工制作的实现。
However, it's not so hard to implement: you can base yourself on startswith keyword and: 但是,实现起来并不难:您可以基于startswith关键字和:
pages = [myQuerySet.filter(myfield__istartswith=i) for i in "ABC...XYZ"] #full alphabet here
(let myQuerySet be, actually, contact_list; let myfield be, actually, first_name). (让myQuerySet实际上是contact_list;让myfield实际上是first_name)。
I've done this once, by adding a letter field to my model and "paginating" manually. 通过将字母字段添加到模型并手动“分页”,我已经完成了一次。 This raised a lot of interesting situation with foreign alphabets.
外国字母引起了很多有趣的情况。 Found this useful: https://pypi.python.org/pypi/Unidecode
发现这个有用: https : //pypi.python.org/pypi/Unidecode
Didn't find any "smarter" way to go about it. 找不到任何“更智能”的解决方法。 But I'd be interested in hearing about more "plug-n-play" solution...
但我会对听到更多“即插即用”解决方案感兴趣……
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.