Ticket #8221: improved_exceptions_for_reverse_with-args.diff

File improved_exceptions_for_reverse_with-args.diff, 2.1 KB (added by mrts, 16 years ago)

Make 'Reverse for foo not found' more useful by showing the arguments as well. This really helps to fix problems in real life.

  • django/core/urlresolvers.py

     
    5252            mod_name, func_name = get_mod_func(lookup_view)
    5353            if func_name != '':
    5454                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]])
    291         raise NoReverseMatch("Reverse for '%s' not found." % lookup_view)
     294        raise NoReverseMatch("Reverse for '%s' with arguments '%s' and keyword "
     295                "arguments '%s' not found." % (lookup_view, args, kwargs))
    292296
    293297    def reverse_helper(self, lookup_view, *args, **kwargs):
    294298        sub_match = self.reverse(lookup_view, *args, **kwargs)
Back to Top