How to sort by list Django

This is a list of jobs and their prices. I need help sorting. I want to make it so that when you click on sort by budget, the list is ordered by budget decrease, and when you click again, it changes to sort by budget increase. I don't know if this requires a new site / findjob / sortbybudget, but I prefer to do it inside the / findjob site. I would appreciate any help or guidance I can read for this.

This is 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)),
)

      

This: 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 %}

      

+3


source to share


3 answers


Instead of reinventing the wheel, the solution I propose is to use a reusable application that does it right.

django-tables2 makes it easy to turn datasets into HTML tables. It has built-in support for pagination and sorting. This is done for HTML tables what django.forms does for HTML forms. eg.

Its features include:

  • Any iteration can be a data source, but special support for Django queries is included.
  • The built-in user interface is independent of JavaScript.
  • Support for automatic table generation based on Django model.
  • Supports custom column functionality through subclasses.
  • Breakdown.
  • Sort a table based on columns.
  • Template tag to enable trivial rendering in HTML.
  • A general view of a mixin for use in Django 1.3.


Screenshot

+2


source


Personally, I would like to use something like jQuery tablesorter to solve this problem. This will allow you to navigate to the server for each view.



Docs: http://tablesorter.com/docs/

+2


source


Another option is to use django filter. Just look at the example application:

https://github.com/alex/django-filter

+1


source







All Articles