from django.shortcuts import redirect def get_all_subclasses(cls): ''' Gets all subclasses recursively, does not contain abstract classes ''' subclass_list = [] for subclass in cls.__subclasses__(): if not subclass._meta.abstract: subclass_list.append(subclass) subclass_list += get_all_subclasses(subclass) return subclass_list def redirect_referer_or(request, url='/'): ''' Returns a django redirect to the requests referer, if there is no referer the redirect will poin to the given url Default url is / ''' referer = request.META.get('HTTP_REFERER') if referer is not None: return redirect(referer) else: return redirect(url)