Ticket #8221: improved_exceptions_for_reverse.diff

File improved_exceptions_for_reverse.diff, 2.0 KB (added by mrts, 7 years ago)
  • django/core/urlresolvers.py

     
    5151            lookup_view = lookup_view.encode('ascii')
    5252            mod_name, func_name = get_mod_func(lookup_view)
    5353            if func_name != '':
    54                 lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
     54                lookup_view = getattr(__import__(mod_name,{}, {}, ['']), func_name)
     55                if not callable(lookup_view):
     56                    raise AttributeError("'%s' in module '%s' is not a callable."
     57                            % (func_name, mod_name))
    5558        except (ImportError, AttributeError):
    5659            if not can_fail:
    5760                raise
     
    197200        try:
    198201            lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
    199202        except (ImportError, AttributeError):
    200             raise NoReverseMatch
     203            raise NoReverseMatch("Error importing '%s': %s." % (viewname, e))
    201204        if lookup_view != self.callback:
    202             raise NoReverseMatch
     205            raise NoReverseMatch("Imported '%s' doesn't match the expected function." % viewname)
    203206        return self.reverse_helper(*args, **kwargs)
    204207
    205208    def reverse_helper(self, *args, **kwargs):
     
    284287    def reverse(self, lookup_view, *args, **kwargs):
    285288        try:
    286289            lookup_view = get_callable(lookup_view, True)
    287         except (ImportError, AttributeError):
    288             raise NoReverseMatch("'%s' is not a callable." % lookup_view)
     290        except (ImportError, AttributeError), e:
     291            raise NoReverseMatch("Error importing '%s': %s." % (lookup_view, e))
    289292        if lookup_view in self.reverse_dict:
    290293            return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view]])
    291294        raise NoReverseMatch("Reverse for '%s' not found." % lookup_view)
Back to Top