Could you move the import statement to the top of the file and do

```python
try:
    from rq_scheduler import Scheduler
except ImportError:
    Scheduler = None
```

Then you don't need `get_scheduler` and can simply do in `get_context_data`:

```python

if Scheduler:
    scheduler = Scheduler(connection=self.connection)
    ctx['scheduler'] = scheduler
    all_jobs = …
    # etc etc
```

And in the detail view:

```python
if Scheduler is None:
    raise Http404
```