title: Sortable tables in Django
body: <p>I read some of the other posts about this and some recommendations involved javascript and using other libraries.  I did something quick by hand, but I'm new to Django and Python for that matter so I'm curious if this isn't a good way to do it.</p>
  
  <p>HTML</p>
  
  <pre><code> &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;a href="?sort=to"&gt;To&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;&lt;a href="?sort=date"&gt;Date&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;&lt;a href="?sort=type"&gt;Type&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          {% for record in records %}
          &lt;tr&gt;&lt;td&gt;{{record.to}}&lt;/td&gt;&lt;td&gt;{{record.date}}&lt;/td&gt;&lt;td&gt;{{record.type}}&lt;/td&gt;&lt;/tr&gt;
          {% endfor %}
      &lt;/table&gt;
  </code></pre>
  
  <p>View</p>
  
  <pre><code>headers = {'to':'asc',
           'date':'asc',
           'type':'asc',}
  
  def table_view(request):
      sort = request.GET.get('sort')
      if sort is not None:
          if headers[sort] == "des":
              records = Record.objects.all().order_by(sort).reverse()
              headers[sort] = "asc"
          else:
              records = Record.objects.all().order_by(sort) 
              headers[sort] = "des"
      else:
          records = Record.objects.all()
      return render_to_response("table.html",{'user':request.user,'profile':request.user.get_profile(),'records':records})
  </code></pre>