Can custom Django filters access request.user?

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

Obviously, I can pass the user as an argument, but if you could just grab the current user, that would be more convenient.

+6
source share
2 answers

Django filters do not receive any special access to the context from which they are called, they are just old old functions.

You need to pass everything that you want to use in this function.

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

+4
source

See my answer here:

fooobar.com/questions/362242 / ...

But, in short, you can access context from a custom filter by retrieving it from the call stack when and only when the filter is called during rendering.

This is admittedly a haphazard solution. Let the buyer be vigilant.

0
source

Source: https://habr.com/ru/post/892701/


All Articles