Object orientation affords elegance and readability for back-end view logic for Python web applications. Come learn about how to not only start using Django’s Class-Based Views, but also how they work and how to break them down for your use case.
When first learning the beloved web framework Django, it’s not uncommon to utilize function-based views for the back-end view logic of an application. This pattern rhymes with other popular web frameworks such as Flask and Pyramid. However, class-based views unlock an important set of capabilities: they help DRY up view logic, encourage consistency in form validation, and “plug" right into database model classes. While it’s easy to appreciate this from an objective standpoint (who wouldn’t want all of those things?), after an initial attempt at incorporating a class-based view, it can be hard to know if you’re making full use of them.
In this talk, we’ll start by replacing a basic function-based view for a homepage with a CBV using of Django’s base generic classes: TemplateView. Next, we’ll continue by looking at the purpose, descendants, and exciting possibilities of one of Django’s more generic views— the ListView class. Lastly, I’ll attempt to debunk the myth that CBVs are only appropriate for straightforward, model-oriented pages, but aren’t as customizable or elegant as function-based views for pages with multiple database components.
Meggie is currently the Director of the Software Engineering Fellowship at Hackbright Academy, where women learn to code in Python, build dynamic web applications, and study computer science in order to start careers in tech. She started coding in Python as a hobby after graduating from UC Berkeley in 2012 with a degree in Gender and Women's Studies. In 2013, Meggie completed Hackbright's Fellowship, and began working at Hackbright as a Software Engineer on internal tools. When not coding or hanging out in the Hackbright classroom, Meggie enjoys taking dance classes at various Bay Area dance schools.