Git Product home page Git Product logo

django-sorter's Introduction

django-sorter

Jazzband

django-sorter helps with sorting objects in Django templates without modifying your views, can be used multiple times on the same page or template, provides helpers to easily generate links and forms to switch the sorting criteria (including the sort order) and has ~100% test coverage.

Quickstart

  1. Get the app with your favorte Python packaging tool, e.g.:

    pip install django-sorter
  2. List this application in the INSTALLED_APPS setting. Your settings file might look something like:

    INSTALLED_APPS = (
        # ...
        'sorter',
    )
  3. If it's not already added in your setup, add the request template context processor to the TEMPLATE_CONTEXT_PROCESSORS setting (you might need to add it):

    TEMPLATE_CONTEXT_PROCESSORS = (
        # ...
        'django.core.context_processors.request',
    )
  4. Specify the allowed sorting criteria, for at least the default 'sort' sorting querystring parameter:

    SORTER_ALLOWED_CRITERIA = {
        'sort': ['first_name', 'creation_date', 'title'],
    }
  5. Add this line at the top of your template to load the sorting tags:

    {% load sorter_tags %}
  6. Decide on a variable that you would like to sort, and use the sort tag on that variable before iterating over it.

    {% sort objects as sorted_objects %}
  7. Optionally, you can display different sort links or forms:

    <tr>
       <th>{% sortlink by "first_name" %}By first name{% endsortlink %}</th>
       <th>{% sortlink by "creation_date,-title" %}By creation date and title{% endsortlink %}</th>
        ...
    </tr>

    The template tag takes a comma separated list of sorting statements. It also is a block tag and allows you to set the label of the generated link. The previous snippet will be rendered like this:

    <tr>
        <th><a href="/?sort=first_name" title="Sort by 'first_name' (asc)">By name</a></th>
        <th><a href="/?sort=creation_date,-title" title="Sort by 'creation_date' (asc) and 'title' (desc)">By creation and title</a></th>
        ...
    </tr>

    Similarly the {% sortform %} template tag renders a form instead of a simple link.

django-sorter's People

Contributors

dsummersl avatar jezdez avatar rdil avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.