Accessing attributes of the current view in Django middleware?

161 views
Skip to first unread message

alk...@gmail.com

unread,
Dec 27, 2013, 3:39:49 PM12/27/13
to django...@googlegroups.com

I'm trying to access an attribute of the current view instance in the middleware layer.

For example, given a class-based view like this:

# views.py
class MyView(View):
    my_attribute = 'something'

I'd love to be able to get a handle on my_attribute in the middleware by doing something like this:

# middleware.py
def process_view(self, request, view_func, view_args, view_kwargs):
    my_attribute = request.view.my_attribute

Of course, this does not work because Django doesn't expose the view instance through the request object.

Is there any way to get this accomplished?

Many thanks!

Mario Gudelj

unread,
Dec 28, 2013, 4:46:18 AM12/28/13
to django...@googlegroups.com

Since the request is simply a dictionary, can you not append that attribute to it?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0dbc397b-0d9b-4382-be8c-a55f818e4ec1%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

thinkin...@gmail.com

unread,
Dec 28, 2013, 8:19:22 AM12/28/13
to django...@googlegroups.com
Hey,

Try along these lines:

    def process_view(self, request, view_func, view_args, view_kwargs):
        view = get_class(view_func.__module__, view_func.__name__)
        view.my_attribute

While `get_class()`:

from django.utils import importlib


def get_class(module_name, cls_name):
    try:
        module = importlib.import_module(module_name)
    except ImportError:
        raise ImportError('Invalid class path: {}'.format(module_name))
    try:
        cls = getattr(module, cls_name)
    except AttributeError:
        raise ImportError('Invalid class name: {}'.format(cls_name))
    else:
        return cls

Robert

alk

unread,
Dec 28, 2013, 1:44:16 PM12/28/13
to django...@googlegroups.com
Thanks a lot for the replies.

On Saturday, December 28, 2013 4:46:18 AM UTC-5, somecallitblues wrote:

Since the request is simply a dictionary, can you not append that attribute to it?

The view hasn't been called yet at that point in the middleware stack, so I don't think it would have had an opportunity to modify the request dictionary:

But I think your suggestion would probably work great to grab the attribute in the `process_response()` or `process_template_response()` middleware hooks.

On Saturday, December 28, 2013 8:19:22 AM UTC-5, thinkin...@gmail.com wrote:
Hey,

Try along these lines:

    def process_view(self, request, view_func, view_args, view_kwargs):
        view = get_class(view_func.__module__, view_func.__name__)
        view.my_attribute

Heh, yeah, this seems to be the only way.  I was kind of headed in this direction but I'm fairly new to Django/python, and to programming in general.  Is this safe/efficient?  I guess this just involves importing the view class once per request -- doesn't seem like a big deal but would love some feedback.

Thanks again!

thinkin...@gmail.com

unread,
Dec 28, 2013, 2:46:51 PM12/28/13
to django...@googlegroups.com


On Saturday, December 28, 2013 6:44:16 PM UTC, alk wrote:

On Saturday, December 28, 2013 8:19:22 AM UTC-5, thinkin...@gmail.com wrote:
Hey,

Try along these lines:

    def process_view(self, request, view_func, view_args, view_kwargs):
        view = get_class(view_func.__module__, view_func.__name__)
        view.my_attribute

Heh, yeah, this seems to be the only way.  I was kind of headed in this direction but I'm fairly new to Django/python, and to programming in general.  Is this safe/efficient?  I guess this just involves importing the view class once per request -- doesn't seem like a big deal but would love some feedback.

Thanks again!

Is it safe: well, as far as I can tell there is no reason it should not be. In terms of efficiency, it's always a little bit an overhead to every request, no matter you need it or not. Have a look at this code:


There's some caching done etc. And it's been tested in production. I had to retreat from that solution for a few reasons (one of them was making the permissions available for other apps or frameworks), but I've spent several hours looking for a better approach with not much success.

Good luck,
R.

alk

unread,
Dec 28, 2013, 3:14:29 PM12/28/13
to django...@googlegroups.com
This is great, and I love the views_cache idea.  Thanks a lot Robert -- much appreciated!
Reply all
Reply to author
Forward
0 new messages