About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Django Unleashed
Andrew Pinkham
800 East 96th Street, Indianapolis, Indiana 46240 USA
Django Unleashed
Copyright 2016 by Pearson Education, Inc.
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.
ISBN-13: 978-0-321-98507-1
ISBN-10: 0-321-98507-9
The Library of Congress cataloging-in-publication data is available at http://lccn.loc.gov/2015033839.
Printed in the United States of America
First printing, October 2015
Editor-in-Chief
Mark L. Taub
Acquisitions Editor
Debra Williams Cauley
Development Editor
Chris Zahn
Managing Editor
John Fuller
Project Editor
Elizabeth Ryan
Copy Editor
Carol Lallier
Indexer
John S. Lewis
Proofreader
Linda Begley
Editorial Assistant
Kim Boedigheimer
Cover Designer
Mark Shirar
Compositor
DiacriTech
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an as is basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or programs accompanying it.
Special Sales
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the U.S., please contact .
Contents
Preface
In early 2013, a startup in Austin, Texas, approached me to work on a banking application using Django. My experience with Django was limited: I had tried to use the tool in 2009 but felt that the learning curve was steep. I wanted to give Django a try but did not have enough time to learn how to use it given the projects time constraints (which was fine: we were forced to use PHP anyway). When I looked at Django again in 2013, I discovered that it had become far more accessible. For certain, those four years had seen Django improve by leaps and bounds. However, I had also gained key knowledge working with web frameworks.
At the end of the project in 2013, I was asked by a different group to take what I had learned and teach its engineers how to program Django. I liked the work enough that I started creating a series of videos based on the material. During a test showing of the videos, one of my reviewers casually commented that the material would be more suitable and more approachable as a book. I still have a hard time believing that such an innocent comment resulted in a year and a half of such intense work, but that is the origin of this book: an off-hand comment.
This book is the book I wish Id had in 2009 and in 2013. It is a how-to book that teaches you how to build a webpage from scratch using Django. The first part of the book (the first 12 chapters) are for my 2009 self. It answers the basic questions that I had when I started learning Django, and it explains the basics of web frameworks and websites. I think of the remaining chapters as a response to my 2013 self. They address the needs of more experienced users. Related materials are available at https://django-unleashed.com
. I hope you find this book useful.
Is This Book for Me?
This book is meant for two types of people:
Programmers who have never built a website before and do not know how web frameworks operate
Programmers who have dabbled or used the basics of Django, and who would like to hone their skills and take advantage of Djangos intermediate features
The book thus caters to both beginners and intermediate users. The only knowledge assumed is basic programming knowledge and Python.
What This Book Contains
This book is a hands-on, single example: we build and deploy a fully functional website over the course of the 30 chapters. Each chapter covers a single part of Django and is the logical next step to building our website while learning how to use Django.
, is an introduction to websites, web frameworks, and Django. We assume knowledge of programming and Python, but absolutely no knowledge of the internals of back-end web programming. In these first 12 chapters, we use the core parts of Djangothe parts used in (almost) every websiteto create the basics of our website. This includes interacting with a database, sending HTML to visitors, and accepting user input in a safe manner.
, we will see how to integrate CSS into our website, shorten our code through generic behavior, and add user authentication to our website.
, we consider what we would have done differently in our project had we known at the beginning what we now know at the end of the book.
Conventions Used in This Book
This book is written with a bottom-to-top approach, meaning we start with a lower level of abstraction (more details) and gradually move up the abstraction ladder (shorter but more opaque code). If you would prefer to learn with a top-to-bottom approach, I recommend reading , and starting each chapter with the last section of the chapter, titled Putting It All Together throughout the book.