这是工作及其价格的列表。 我需要排序方面的帮助。我想做到这一点,当您单击按预算排序时,列表按预算减少排序,再次单击时更改为按预算增加排序。我不知道这是否需要一个新页面 site/findjob/sortbybudget,但我更喜欢在 site/findjob 中这样做。我将不胜感激我可以阅读的任何帮助或引用资料。
这是 urls.py
from django.conf.urls.defaults import patterns, include, url
from project.preview.models import Task
info_task = {
'queryset': Task.objects.all(),
'template_name': 'template.html',
}
urlpatterns = patterns('',
(r'^findtask/$', 'django.views.generic.list_detail.object_list', dict(info_task)),
)
这是:template.html
<div class="sortList">
**Sort by:**
<ul>
<li class="sort"><a href=""/>Budget</a></li>
<li class="sort"><a href=""/>Newly Added</li>
</ul>
</div>
<div class="taskListCon">
{% for object in object_list %}
<div class="taskCon">
<div class="Title">
{{ object.title_description }}
</div>
<div class="clientID">
{{object.userid}}
</div>
<div class="Price">
Budget:{{ object.max_budget }}
</div>
<div>
{% endfor %}
最佳答案
我建议的解决方案不是重新发明轮子,而是享受一个可重复使用的正确应用程序。
django-tables2 simplifies the task of turning sets of data into HTML tables. It has native support for pagination and sorting. It does for HTML tables what django.forms does for HTML forms. e.g.
Its features include:
- Any iterable can be a data-source, but special support for Django querysets is included.
- The builtin UI does not rely on JavaScript.
- Support for automatic table generation based on a Django model.
- Supports custom column functionality via subclassing.
- Pagination.
- Column based table sorting.
- Template tag to enable trivial rendering to HTML.
- Generic view mixin for use in Django 1.3.
关于python - 如何在 Django 列表中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9989681/