1
{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages %}
    <li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>

The code above will render a simple unordered list containing flatpages. I would like to sort them using the "index" property of each flatpage, instead of alphabetically by the "url" property (as it is done by default).

jerivas
  • 243
  • 2
  • 16

2 Answers2

3

Since you can't (should not) change the core model of flatpages you can inherit it:

CustomFlatpage(Flatpage)
    # Possible extra fields, add methods or your own object manager
    class Meta:
         ordering = ['somefield']

If you want a simplified approach, could just write a templatetag that (re)sorts your queryset. This might cause some overhead but unless your site is high traffic or contains thousands of flatpages this should just be fine.

{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages|your_pre_iteration_shuffle %}
    <li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
3

You should try to write sort filter , or try to use

dictsort

Or write django assignment tag.