[Django]-Using middleware to add arguments to view functions in django

3👍

If you look at the Django middleware documentation, you’ll see;

If process_view returns an HttpResponse object, Django won’t bother calling any other view or exception middleware, or the appropriate view; it’ll apply response middleware to that HttpResponse, and return the result.

So returning an HttpResponse will skip the execution of other middleware classes below this one which in general should be avoided unless your middleware is the last one in settings.MIDDLEWARE_CLASSES list.

However, you still can bind your API object to HttpRequest instance passed on to middleware. It is the same approach to what AuhenticationMiddleware does in order to populate request.user.

def APIMiddleware(object):
    def process_request(self, request):
        request.api = API(host, port, user, password, extra={"url": request.get_full_path()})

2👍

you can just change view_kwargs in middleware

class SomeMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        view_kwargs['my_api'] = api(some, args, here)
        return None

def my_view(request, my_api):
    # you can use you api there
def my_view(request, args, my_api)
    # you can use you api there

document it’s there
middleware returns None, Django will continue processing this request, executing any other process_view() middleware.

but, this only applies to every view function can got the keyword argument ‘myapi’, otherwise will raise the TypeError.

so the best way isn’t pass your api by func arguments, like @ozgur pass your api by request.

1👍

You can use a middleware but there are two other possibilities too, both more flexible. The first one is to use a decorator and wrap the view:

@my_api_init_decorator
def my_view(request, my_api):
    ...

This allows you to explicitly select views, check user authorization or permissions before you init your api…

The second solution is to use class based views and create your own view you inherit from.

👤Bruce

Leave a comment