The HttpRedirectRequestMiddleware class

class fluent_contents.middleware.HttpRedirectRequestMiddleware(get_response=None)

New in version 1.0.

Middleware that handles requests redirects, requested by plugins using ContentPlugin.redirect(). This needs to be added to MIDDLEWARE_CLASSES for redirects to work.

Some background:

Since plugins can’t return a HttpResponseRedirect response themselves, because they are part of the template rendering process. Instead, Python exception handling is used to abort the rendering and return the redirect.

When ContentPlugin.redirect() is called, a HttpRedirectRequest exception is raised. This middleware handles the exception, and returns the proper redirect response.

process_exception(request, exception)

Return a redirect response for the HttpRedirectRequest

process_template_response(request, response)

Patch a TemplateResponse object to handle the HttpRedirectRequest exception too.