tomchristie/django-rest-framework — Issue #166: View's final() not getting called if the handler raises an exception
Author: izquierdo
Created: 2012-02-15 06:05:53+00:00
State: open

I'm having problems in my tests because the `final()` method in `View` will not get called if an exception (other than `ErrorResponse`) is raised from the handler.

The following happens:

* A test in my test suite calls a DRF view expecting to get 404 in return, which it does. The view raises Django's `Http404` to do this. The test is successful.

* Since an exception was raised, `final()` doesn't get called, so the following line in `final()` does not get run:

```python
set_script_prefix(self.orig_prefix)
```

* An unrelated test in my test suite (not using DRF) occurring later fails because the script prefix was not restored.

I'm writing a test case reproducing this and later I'll submit a fix. I'm planning to call `final()` from a `finally` block in `View.dispatch()` (right now it's just called at the end of `dispatch()`.