How do I access the request object in a Django-CMS plugin?
Asked Answered
I

1

15

I have a Django-CMS plugin which is used to display a list of objects. The required functionality of the plugin is that the list is paginated and can be re-ordered based on properties of the objects in the list.

Handling this functionality with ajax is not an ideal solution in this particular case so I was planning on using django Paginator, which requires a 'page' querystring parameter, and passing a 'order' querystring parameter which I would then use to define the order of the queryset.

The problem is that I can't see anyway of accessing the request object from within the plugins render function.

Does anyone know if it's possible to access the request object from within the render function, or can suggest a workaround?

Instauration answered 12/7, 2012 at 19:13 Comment(0)
E
24

The CMSPluginBase's render method takes a context object. You should be able to access the request via that object if your view is using a RequestContext instance.

class MyCoolPlugin(CMSPluginBase):

    def render(self, context, instance, placeholder):

         #Do something with the request, like access the user
         current_user = context['request'].get('user', None)
         ...
Embrasure answered 12/7, 2012 at 20:28 Comment(1)
You're right. I'm an idiot. I was trying to access it as context.request instead of context['request']. Old habits die hard.Instauration

© 2022 - 2024 — McMap. All rights reserved.