Q. | Is it possible to use middleware to redirect web pages? |
A. | Yes. Instead of returning None from the process_request() function, return a call to the function that renders an HttpResponse. |
Q. | Is it possible to conditionally enable middleware? |
A. | Yes. You can define an __init__(self) function in the middleware application. Put in your code to determine if the application should be enabled or disabled. Then, if the determination is made to disable the middleware, raise a django.exceptions.MiddlewareNotUsed exception. For example:
from django.exceptions import MiddlewareNotUsed class CustomViewMiddleware(object): def __init__(self): if <your condition code>: raise MiddlewareNotUsed |