Two Scoops of Django Best Practices for Django 1.8
Total Page:16
File Type:pdf, Size:1020Kb
Two Scoops of Django Best Practices For Django 1.8 Daniel Roy Greenfeld Audrey Roy Greenfeld Two Scoops of Django: Best Practices for Django 1.8 ird Edition, 2015-05-20 by Daniel Roy Greenfeld and Audrey Roy Greenfeld Copyright ⃝c 2013-2015 Daniel Roy Greenfeld, Audrey Roy Greenfeld, and Two Scoops Press. All rights reserved. is book may not be reproduced in any form, in whole or in part, without written permission from the authors, except in the case of brief quotations embodied in articles or reviews. Limit of Liability and Disclaimer of Warranty: e authors have used their best efforts in preparing this book, and the information provided herein “as is.” e information provided is sold without warranty, either express or implied. Neither the authors nor Cartwheel Web will be held liable for any damages to be caused either directly or indirectly by the contents of this book. Trademarks: Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benet of the trademark owner with no intention of infringement of the trademark. First Printing, April 2015 For more information, visit https://twoscoopspress.com. iii iv Dedication For Malcolm Tredinnick 1971-2013 We miss you. http://2scoops.co/malcolm-tredinnick-memorial v About the Dedication Malcolm Tredinnick wasn’t just a Django core developer and reviewer of “Two Scoops of Django: Best Practices for Django 1.5.” To us, he was much, much more. Daniel had worked with Malcolm Tredinnick in the summer of 2010, but we rst met him in person at DjangoCon 2010. He was funny and charming, sharply opinionated but always a gentleman; we instantly became close friends. In 2012, when we co-organized the rst PyCon Philippines, as soon as we told him about it, Mal- colm instantly declared he was coming. He gave two memorable talks and ran an impromptu all-day Django tutorial. He also pushed and encouraged the local community to work on Filipino language translations for Django, including Tagalog, Tausug, Cebuano, and more. After the conference, we started working on a book about Django best practices. We gathered friends and colleagues to help us as technical reviewers. Malcolm Tredinnick became the most active of them. He was our mentor and forced us to dig deeper and work harder. He did this while working a day job as the leader of a combined Rails and Haskell team; Malcolm was a true programming language polyglot. For our book, he provided so much assistance and guidance we tried to gure out a way to include him in the author credits. When we told him about our dilemma, he laughed it off saying, “For a book called ‘Two Scoops’, you can’t have three authors.” We suggested he share credit with us on a second book, and he refused, saying he preferred to just comment on our work. He said that he wanted people to have proper references, and for him, simply reviewing our work was contributing to the greater good. Eventually the two of us quietly planned to somehow coerce him into being a co-author on a future work. After months of effort, we released the rst iteration on January 17th, 2013. Malcolm stepped back from Two Scoops of Django, but we stayed in touch. Since Malcolm was unable to attend PyCon US 2013 we weren’t sure when we would meet him again. Two months later, on March 17th, 2013, Malcolm passed away. We knew Malcolm for less than three years and yet he made an incredible difference in our lives. We’ve heard many similar stories in the community about Malcolm; He was a friend and mentor to countless others around the world. His last lesson to us went beyond code or writing, he taught us to never take for granted friends, family, mentors, and teachers. Contents Dedication v About the Dedication . vi Authors’ Notes xxvii A Few Words From Daniel Roy Greenfeld . xxvii A Few Words From Audrey Roy Greenfeld . xxviii Introduction xxix A Word About Our Recommendations . xxix Why Two Scoops of Django? . xxx Before You Begin . xxxi is book is intended for Django 1.8 and Python 2.7.x/3.3.3+ . xxxi Each Chapter Stands On Its Own . xxxi Conventions Used in is Book . xxxii Core Concepts . xxxiii Keep It Simple, Stupid . xxxiii Fat Models, Utility Modules, in Views, Stupid Templates . xxxiv Start With Django By Default . xxxiv Be Familiar with Django’s Design Philosophies . xxxiv e Twelve-Factor App . xxxv Our Writing Concepts . xxxv Provide the Best Material . xxxv Stand on the Shoulders of Giants . xxxv Listen to Our Readers and Reviewers . xxxvi Publish Errata . xxxvi 1 Coding Style 1 1.1 e Importance of Making Your Code Readable . 1 vii Contents 1.2 PEP8 ........................................ 2 1.2.1 e 79-Character Limit . 3 1.3 e Word on Imports . 3 1.4 Use Explicit Relative Imports . 4 1.5 Avoid Using Import * . 7 1.6 Django Coding Style . 9 1.6.1 Consider the Django Coding Style Guidelines . 9 1.6.2 Use Underscores in URL Pattern Names Rather an Dashes . 9 1.6.3 Use Underscores in Template Block Names Rather an Dashes . 10 1.7 Choose JS, HTML, and CSS Style Guides . 10 1.7.1 JavaScript Style Guides . 10 1.7.2 HTML and CSS Style Guides . 11 1.8 Never Code to the IDE (Or Text Editor) . 11 1.9 Summary . 12 2 e Optimal Django Environment Setup 13 2.1 Use the Same Database Engine Everywhere . 13 2.1.1 You Can’t Examine an Exact Copy of Production Data Locally . 13 2.1.2 Different Databases Have Different Field Types/Constraints . 14 2.1.3 Fixtures Are Not a Magic Solution . 15 2.2 Use Pip and Virtualenv . 15 2.3 Install Django and Other Dependencies via Pip . 17 2.4 Use a Version Control System . 18 2.5 Optional: Identical Environments . 19 2.5.1 Vagrant and VirtualBox . 19 2.6 Summary . 20 3 How to Lay Out Django Projects 21 3.1 Django 1.8’s Default Project Layout . 21 3.2 Our Preferred Project Layout . 22 3.2.1 Top Level: Repository Root . 23 3.2.2 Second Level: Project Root . 23 3.2.3 ird Level: Conguration Root . 23 3.3 Sample Project Layout . 24 3.4 What About the Virtualenv? . 27 3.5 Going Beyond startproject ........................... 28 viii Contents 3.5.1 Generating Project Boilerplate With Cookiecutter . 29 3.5.2 Our Favorite Project Template . 30 3.5.3 An Alternative Template: django-kevin ................. 31 3.5.4 Other Alternatives . 31 3.6 Summary . 32 4 Fundamentals of Django App Design 33 4.1 e Golden Rule of Django App Design . 34 4.1.1 A Practical Example of Apps in a Project . 35 4.2 What to Name Your Django Apps . 36 4.3 When in Doubt, Keep Apps Small . 37 4.4 What Modules Belong in an App? . 37 4.4.1 Common App Modules . 37 4.4.2 Uncommon App Modules . 38 4.5 Summary . 40 5 Settings and Requirements Files 41 5.1 Avoid Non-Versioned Local Settings . 42 5.2 Using Multiple Settings Files . 43 5.2.1 A Development Settings Example . 46 5.2.2 Multiple Development Settings . 47 5.3 Separate Conguration From Code . 48 5.3.1 A Caution Before Using Environment Variables for Secrets . 49 5.3.2 How to Set Environment Variables Locally . 49 5.3.3 How to Set Environment Variables in Production . 51 5.3.4 Handling Missing Secret Key Exceptions . 52 5.4 When You Can’t Use Environment Variables . 54 5.4.1 Using JSON Files . 54 5.4.2 Using Cong, YAML, and XML File Formats . 55 5.5 Using Multiple Requirements Files . 55 5.5.1 Installing From Multiple Requirements Files . 57 5.5.2 Using Multiple Requirements Files With Platforms as a Service (PaaS) . 57 5.6 Handling File Paths in Settings . 58 5.7 Summary . 60 ix Contents 6 Model Best Practices 63 6.1 Basics . 64 6.1.1 Break Up Apps With Too Many Models . 64 6.1.2 Be Careful With Model Inheritance . 64 6.1.3 Model Inheritance in Practice: e TimeStampedModel . 66 6.1.4 Database Migrations . 67 6.2 Django Model Design . 69 6.2.1 Start Normalized . 69 6.2.2 Cache Before Denormalizing . 69 6.2.3 Denormalize Only if Absolutely Needed . 69 6.2.4 When to Use Null and Blank . 70 6.2.5 When to Use BinaryField . 72 6.2.6 Try to Avoid Using Generic Relations . 73 6.2.7 PostgreSQL-Specic Fields: When to Use Null and Blank . 74 6.3 e Model meta.