Refactoring like a Ninja: Using dictionaries(multi-purpose)
Hello i will share some ways on how to re-factor your codes in Django using dictionaries. Gonna post this here as my notes and for future references
have you experienced coding a views.py and you pass alot of context to the template( yet they all do the same but uses different model for example). Here is a sample code in your views.py that you can re-factor using dictionaries.
Here's how i made the code simpler and cleaner using dictionary.
* if you noticed the dictionary model_dic has dual purpose. the value is for the Models( of course) but for the keys, I also used them as the context name( cool idea right?). You won't notice much of a difference between the two code but if you have for example 10 types of news then this would work pretty cool.
That's all!.. hope this would help someone out there xD.
*note: this code do not exist ( i just made it for this post ) and the idea of this is based from Earvin(that's me) -thumbs up-.














