Can custom Django filters access request.user?

dcrosta picture dcrosta · Jul 13, 2011 · Viewed 7.9k times · Source

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.

Answer

Chris W. picture Chris W. · Jul 13, 2011

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/