From 30b17f94a33d8be0704c13d4f35477da8706e4f4 Mon Sep 17 00:00:00 2001 From: Sanj Date: Tue, 12 Jun 2012 02:10:24 +0530 Subject: [PATCH] fix 500 on empty list returned --- itf/app/models.py | 14 ++++++++------ itf/insidepages/views.py | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/itf/app/models.py b/itf/app/models.py index b840192..e40cc8f 100755 --- a/itf/app/models.py +++ b/itf/app/models.py @@ -288,11 +288,7 @@ class ItfModel(models.Model): ret = [] page_no = options['page'] list_size = options['count'] - try: - qset = kls.get_qset() - except: - qset = kls.objects.all() - + qset = kls.get_qset() search = options['search'] if search != '': @@ -320,6 +316,11 @@ class ItfModel(models.Model): except: pass + hasResults = True + if qset.count() == 0: + hasResults = False + qset = kls.get_qset() + ''' r0 = options['range'][0] r1 = options['range'][1] @@ -343,6 +344,7 @@ class ItfModel(models.Model): 'has_next': results.has_next(), 'has_previous': results.has_previous(), 'page_no': page_no, + 'has_results': hasResults, 'num_pages': paginator.num_pages, 'items': ret } @@ -352,7 +354,7 @@ class ItfModel(models.Model): ''' Override this method in your model class to define a custom queryset instead of objects.all(), for instance, to always exclude unpublished items. ''' - raise NotImplementedError + return kls.objects.all() def getField(fields, name): for f in fields: diff --git a/itf/insidepages/views.py b/itf/insidepages/views.py index 33086ba..c752ec5 100755 --- a/itf/insidepages/views.py +++ b/itf/insidepages/views.py @@ -112,12 +112,20 @@ def render_object(request, module_slug): } object_list = tab.get_list(list_options) - if object_id == 0: + if object_id == 0: object_id = object_list['items'][0]['id'] - - obj = get_object_or_404(model_class, pk=object_id) - item_data = obj.insidepage_dict(request) - + + if object_list['has_results']: + obj = get_object_or_404(model_class, pk=object_id) + item_data = obj.insidepage_dict(request) + else: #Should be a better way to tell the template a search returned no result + obj = { + 'get_title': 'No Results Found' + } + item_data = { + 'html': 'The search query you entered did not return any results.' + } + context = RequestContext(request, { 'item': obj, 'item_data': item_data,