I’m moving from a PHP background into Django development via python, mostly for the sake of tackling a MVC (or MVT) that I feel makes the most sense, although in this pattern I’ve started to notice a lot of repeated code in my views.
For example, when logged in I have information regarding the user that I would like to appear on every page, although when using render_to_response and in every view this is required I have to grab the information and pass it to the render_to_response function.
I’m wondering what would be the most efficient way to cut down on the duplicate code which would in essence be required in all views in a particular app.
Thanks in advance.
Personally I am a huge fan of decorators, which are a python feature that isn’t specific to Django. Decorators are the perfect syntactic sugar on top of higher-order functions, and they’re especially useful for reducing boilerplate in views — you can quickly define a generalized wrapper function, in which you can put the repetitive code for easy reuse and convenient one-stop refactoring.
It’s probably easier to show you than explain how they work. Here is a simplified view example:
… but then say you wanted to make these pages require the user to log in. You might add login code like so:
… which is starting to get notably bigger and repetitive, even for a contrived example. You can make your functions slim again with decorators, like so:
from decorator import decorator
What happens is the decorator function is executed at the time of the function’s definition. The ‘f’ in my example is an object representing the function that the decorator is applied to, which you can manipulate in unending ways.
This requires the decorator library, which is free on PyPI as are many good python morsels, you’ll find.
You don’t need the this library to write decorator functions, but it’s helpful, especially in the beginning. They can do a whole lot more — any callable can be a decorator; you can decorate class methods and intercept the
selfvariable; decorators can be chained up, like so:I’ll leave the exploration of what you can do with such easy higher-order function manpipulation for you, should this notion whet your appetite. I have many more examples as well, for you or any other curious new python aficionados. Good luck.