Skip to content

Commit

Permalink
Handle exception: What if the order_by doesn't work (not a real field…
Browse files Browse the repository at this point in the history
… to sort on for eg.)?
  • Loading branch information
directeur committed Apr 13, 2009
1 parent 81ce973 commit 0867e9e
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion django_sorting/templatetags/sorting_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

DEFAULT_SORT_UP = getattr(settings, 'DEFAULT_SORT_UP' , '↑')
DEFAULT_SORT_DOWN = getattr(settings, 'DEFAULT_SORT_DOWN' , '↓')
INVALID_FIELD_RAISES_404 = getattr(settings,
'SORTING_INVALID_FIELD_RAISES_404' , False)

sort_directions = {
'asc': {'icon':DEFAULT_SORT_UP, 'inverse': 'desc'},
Expand Down Expand Up @@ -93,7 +95,13 @@ def render(self, context):
value = self.queryset_var.resolve(context)
order_by = context['request'].field
if len(order_by) > 1:
context[key] = value.order_by(order_by)
try:
context[key] = value.order_by(order_by)
except template.TemplateSyntaxError:
if INVALID_FIELD_RAISES_404:
raise Http404('Invalid field sorting. If DEBUG were set to ' +
'False, an HTTP 404 page would have been shown instead.')
context[key] = value
else:
context[key] = value

Expand Down

0 comments on commit 0867e9e

Please sign in to comment.