Learning Django Web Development
Total Page:16
File Type:pdf, Size:1020Kb
www.it-ebooks.info Learning Django Web Development From idea to prototype, a learner's guide for web development with the Django application framework Sanjeev Jaiswal Ratan Kumar BIRMINGHAM - MUMBAI www.it-ebooks.info Learning Django Web Development Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2015 Production reference: 1150615 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78398-440-4 www.packtpub.com www.it-ebooks.info Credits Authors Copy Editors Sanjeev Jaiswal Brandt D'mello Ratan Kumar Neha Vyas Reviewers Project Coordinator Michael Giuliano Nikhil Nair Danijel Pančić Martin Pernica Proofreader Safis Editing Vikash Verma Indexer Commissioning Editor Mariammal Chettiyar Julian Ursell Production Coordinator Acquisition Editors Nilesh R. Mohite Nikhil Karkal Larissa Pinto Cover Work Nilesh R. Mohite Content Development Editor Arun Nadar Technical Editor Abhishek R. Kotian www.it-ebooks.info About the Authors Sanjeev Jaiswal is a computer graduate with 5 years of industrial experience. He basically uses Perl and GNU/Linux for his day-to-day work. He also teaches Drupal and WordPress CMS to bloggers. He first developed an interest in web application penetration testing in 2013; he is currently working on projects involving penetration testing, source code review, and log analysis, where he provides the analysis and defense of various kinds of web-based attacks. Sanjeev loves teaching technical concepts to engineering students and IT professionals and has been teaching for the last 6 years in his leisure time. He founded Alien Coders (http://www.aliencoders.org), based on the learning through sharing principle for computer science students and IT professionals in 2010, which became a huge hit in India among engineering students. He usually uploads technical videos on YouTube under the Alien Coders tag. He has got a huge fan base at his site because of his simple but effective way of teaching and his philanthropic nature toward students. You can follow him on Facebook at http://www.facebook.com/aliencoders and on Twitter at @aliencoders. He wrote Instant PageSpeed Optimization, Packt Publishing, and looks forward to authoring or reviewing more books for Packt Publishing and other publishers. Ratan Kumar is a computer science and engineering graduate with more than a year of start-up experience. He received the Technical Excellence Memento from the Association of Computer Engineering Students (ACES), Cochin University of Science and Technology. When he was a product engineer at Profoundis, he worked on an international project based on services using Django. He was also part of the Microsoft accelerator program with Profoundis that was responsible for building the product iTestify, which was built using Django. www.it-ebooks.info He then moved to Tracxn!, an organization that works on building platforms that can help venture capitalists, investment banks, and corporate developers find new and interesting start-ups in their investment sector. As a software developer and engineer, he majorly contributed to the development of the core product platform of Tracxn using Python for the initial scraping work, such as building cron scrappers to crawl millions of pages daily, cleaning them up, and analyzing them. He also built the company's first product, which is called Tracxn Extension—a Chrome extension using AngularJS. He contributed to the Tracxn product platform using Grails as the framework. He also worked on Bootstrap—a frontend framework—to design the home page of tracxn.com. www.it-ebooks.info About the Reviewers Michael Giuliano has been programming software in various languages and technologies for the past 15 years. Having used Python in the fields of web services, machine learning, and big data since 2008, he finds it to be one of the most versatile, elegant, and productive programming languages. Michael is currently based in London, where he leads the Python development team at Zoopla Property Group Plc. Danijel Pančić is a JavaScript ninja and a passionate Django enthusiast. He is currently working at Bitstamp as a senior developer. He also works on various projects, including online games, and experiments with new approaches and techniques in search of better ways to achieve the desired results. You can find him at http://www.panco.si/. Martin Pernica is currently a lead programmer and a cofounder of a new game studio called Soulbound Games in Czech Republic. He started programming very young on old PCs and, after that, he started working mainly as a web developer on PHP, Python, and Ruby for various companies. After some years of web development, Martin switched to the game development industry and started his own game studio. He also started teaching at local universities on mobile, web, and game development. He always tries to look under the hood of problems and challenges and then solves and optimizes them, which is still his passion. www.it-ebooks.info Vikash Verma is a young and enthusiastic software professional who has had a wide exposure to open source technologies. His experience involves both client-side programming and server-side programming through Python, Django, and many other demanding technologies. He has been a vital part of interesting projects from start-ups to leading IT companies as an individual leader. He has experience in the fields of data analytics, web crawling, web scraping, web application development, automation, ETL, and many more technical tracks. I would like to thank my family and peers who always inspired me to be a go-getter. Not to mention the support and motivation I get from my soul mate, Smriti, who ensures that my work and life are in perfect balance with each other. www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info Table of Contents Preface vii Chapter 1: Introduction to Django 1 Why web development in the first place? 2 What has changed in web development 2 The MVC pattern in web development 3 Multilingual support 4 Why Django? 4 Inside Django 5 Django is mature 5 Batteries included 5 Tight integration between the component and modular framework 5 Object-relational mapper 6 Clean URL design 6 Automatic administration interface 6 Advanced development environment 6 What's new in Django 1.6 and 1.7 6 Supported databases 7 What you will learn using this book 8 Summary 9 Chapter 2: Getting Started 11 Installing the required software 11 Installing Python 11 Installing Python on Windows 12 Installing Python on Unix/Linux 13 Installing Python on Mac OS X 14 Installing virtualenv 14 Installing Django 17 Django compatibility with operating systems – Windows versus Linux 17 Installing Django on Windows 18 Installing Django on Unix/Linux and Mac OS X 18 Installing a database system 19 [ i ] www.it-ebooks.info Table of Contents Creating your first project 20 Creating an empty project 20 Setting up the database 21 Launching the development server 25 Summary 26 Chapter 3: Code Style in Django 27 Django coding style 28 Understanding indentation in Python 28 Doing indentation right – do we need four spaces per indentation level? 29 The importance of blank lines 30 Importing a package 31 Grouping imported packages 31 Naming conventions in Python/Django 32 Using IDE for faster development 33 Setting up your project with the Sublime text editor 33 Setting up the PyCharm IDE 36 The Django project structure 37 Best