Conceived on Jul 30, 2014
/ Series:
python,
django
No longer are your methods confined to bare calls!
One of my pet peeves when it comes to Django is that you can’t call methods that require arguments in templates. While this is fine most of the time, it does mean that you need to have one property or method per call you want to make, which sometimes gets very cumbersome.
I needed a way to define various dynamic permissions that are calculated at runtime (for irrelevant reasons, Django’s permissions framework wasn’t a good fit), and writing properties like can_register
, can_add_tags
, can_subscribe
got tedious. These tended to be defined all over the place, rather than in one central spot, and it was hard to add more checks without cluttering the classes.
I would much prefer to have a single method (let’s call it can()
) that accepted a string with the permission I wanted to check, and return True
or False
, depending. This is easy to do in the views, but templates would never be able to call it with an argument.
However, since Django can do dictionary-style attribute lookups, I could add a dictionary interface over the method, and allow
Continue reading…