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.
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.
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/
@register.simple_tag(takes_context=True)
–
Ladino See my answer here:
https://mcmap.net/q/603986/-django-accessing-the-requestcontext-from-within-a-custom-filter
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.
© 2022 - 2024 — McMap. All rights reserved.