Thursday, October 16, 2008

SAY NO TO FLASH POPUPS!

I've been a Firefox user for a long time.

After Google Chrome has been published I decided to check it out.
After 1 day I had to switch back to Firefox.

The reason?
https://addons.mozilla.org/firefox/addon/10 (Firefox AdBlock plugin)

It's IMPOSSIBLE to watch the web without this plugin.
Flash plugins everywhere ... This is horrible.

People! Use this plugin, do not let manipulate and waste your time on
pages using this harmful type of advertising.

Boycote it! Let others know to not use it!
Admins, block ad hosts too!

I do not mean to fight with ads. I can watch ads, as long as it does not eat 100% of my CPU and some fuckin* flash poup appears! AARGH!!

THIS IS CRAZY!

Thursday, October 9, 2008

django-tables generic way for displaying tables (pagination included)

In thnis short post I would like to let you know about the great project
django-tables.

You can read more about it at:
http://blog.elsdoerfer.name/2008/07/09/django-tables-a-queryset-renderer/

In a couple of words it lets you:
- display the table using one template which lets you sort & paginate easily (filtering in plans!)

By reading the text below you will find out how to create table with sort & pagination quickly.

In usage it's very similar do django newforms (forms as of 1.0)
Example: (views.py)

class MyTable(tables.ModelTable):

  class Meta:
      model = SomeModel

def view_something(request):
  table = MyTable(queryset, order_by=request.GET.get('sort','some_field))

  return render_to_response('templates/table.html', {'table': table, 'rows' : table.rows})
# we need to return table, and  rows separately to use django-pagination (though django-tables provides its own pagination mechanism)

Now let's get to template:

{% load pagination_tags %}
{% autopaginate rows 10 %}



  {% for column in table.columns %}

  {% endfor %}


{% for row in rows %}

  {% for value in row %}

{% endfor %}


{% if column.sortable %}
{{ column }}
{% if column.is_ordered_reverse %}

{% else %}

{% endif %}
{% else %}
{{ column }}
{% endif %}
{{ value }}
{% endfor %}
{% paginate %}


That's it!

This way you get generic way of displaying tables + sorting ability.
By default, django-tables takes field names for table headers when using ModelTable (verbose_name is in plans as well).
You can exclude displaying fields too!

As for now, you can specify your own names in MyTable class.

Let's get back to our class:

class MyTable(tables.ModelTable):
  class Meta:
      model = SomeModel

  field1 = tables.Column(name="Name")
  field_we_want_to_exclude = tables.Column(visiable=Fale, sortable=False)


NOTE:
Django tables is also aware of ForeignKey relations.

Check it out!