Can custom Django filters access request.user?

爱⌒轻易说出口 提交于 2019-12-23 07:44:25

问题


Is it possible to access the current User (i.e. user in the template context) from a custom template filter?

Obviously I can pass the user in as an argument, but if it's possible to just grab the current user, that would be more convenient.


回答1:


Django filters aren't given any special access to the context from which they are called, they're just plain old functions.

You'll need to pass in anything you want to use within the function.

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/




回答2:


See my answer here:

https://stackoverflow.com/a/28098279/201945

But, in short, you CAN access the context from within a custom filter by extracting it from the call stack when, and only when, the filter is called during render.

This is, admittedly, a haphazard solution. Caveat emptor.



来源:https://stackoverflow.com/questions/6683466/can-custom-django-filters-access-request-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!