如何在Django-CMS插件中访问请求对象? [英] How do I access the request object in a Django-CMS plugin?

查看:277
本文介绍了如何在Django-CMS插件中访问请求对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django-CMS插件,用于显示对象列表。插件所需的功能是列表是分页的,可以根据列表中对象的属性重新排序。



使用ajax处理此功能不是在这种特殊情况下,一个理想的解决方案是我计划使用django Paginator,它需要一个'page'querystring参数,并且传递一个'order'querystring参数,然后我将使用它来定义查询集的顺序。



问题是我无法看到从插件渲染功能中访问请求对象。



是否有人知道是否可以从render函数中访问请求对象,或者可以提出解决方法?

解决方案

CMSPluginBase的渲染方法取一个上下文对象。如果您的视图正在使用RequestContext实例,您应该可以通过该对象访问请求。

 类MyCoolPlugin(CMSPluginBase): 

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

#Do有请求的东西,像访问用户
current_user = context ['request' ] .get('user',None)
...


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?

解决方案

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)
         ...

这篇关于如何在Django-CMS插件中访问请求对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆