This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
Overview
The aim of this book is to provide you with a practical guide to web development using Django and Python. The book is designed primarily for students, providing a walkthrough of the steps involved in getting a web application up and running with Django.
This book seeks to complement the official Django Tutorials and many of the other excellent tutorials available online. By putting everything together in one place, this book fills in many of the gaps in the official Django documentation providing an example-based design driven approach to learning the Django framework. Furthermore, this book provides an introduction to many of the aspects required to master web application development (e.g. HTML, CSS, JavaScript, etc.).
1.1 Why Work with this Book?
This book will save you time. On many occasions weve seen clever students get stuck, spending hours trying to fight with Django and other aspects of web development. More often than not, the problem was usually because a key piece of information was not provided, or something was not made clear. While the occasional blip might set you back 10-15 minutes, sometimes they can take hours to resolve. Weve tried to remove as many of these hurdles as possible. This will mean you can get on with developing your application instead of stumbling along.
This book will lower the learning curve. Web application frameworks can save you a lot of hassle and lot of time. Well, that is if you know how to use them in the first place! Often the learning curve is steep. This book tries to get you going - and going fast by explaining how all the pieces fit together.
This book will improve your workflow. Using web application frameworks requires you to pick up and run with a particular design pattern - so you only have to fill in certain pieces in certain places. After working with many students, we heard lots of complaints about using web application frameworks - specifically about how they take control away from them (i.e. inversion of control). To help you, weve created a number of workflows to focus your development process so that you can regain that sense of control and build your web application in a disciplined manner.
This book is not designed to be read. Whatever you do, do not read this book! It is a hands-on guide to building web applications in Django. Reading is not doing. To increase the value you gain from this experience, go through and develop the application. When you code up the application, do not just cut and paste the code. Type it in, think about what it does, then read the explanations we have provided to describe what is going on. If you still do not understand, then check out the Django documentation, go to !
1.2 What you will Learn
In this book, we will be taking an exampled-based approach. The book will show you how to design a web application called Rango (). Along the way, well show you how to perform the following key tasks.
- How to setup your development environment - including how to use the terminal, your virtual environment, the
pip
installer, how to work with Git, and more. - Setup a Django project and create a basic Django application.
- Configure the Django project to serve static media and other media files.
- Work with Djangos Model-View-Template design pattern.
- Create database models and use the object relational mapping (ORM) functionality provided by Django.
- Create forms that can utilise your database models to create dynamically generated webpages.
- Use the user authentication services provided by Django.
- Incorporate external services into your Django application.
- Include Cascading Styling Sheets (CSS) and JavaScript within a web application.
- Apply CSS to give your application a professional look and feel.
- Work with cookies and sessions with Django.
- Include more advanced functionality like AJAX into your application.
- Deploy your application to a web server using PythonAnywhere.
At the end of each chapter, we have included a number of exercises designed to push you harder and to see if you can apply what you have learned. The later chapters of the book provide a number of open development exercises along with coded solutions and explanations.
Exercises will be clearly delineated like this!
In each chapter we have added a number of exercises to test your knowledge and skill.
You will need to complete these exercises as the subsequent chapters are dependent on them.
Dont worry if you get stuck, though, as you can always check out our solutions to all the exercises on our GitHub repository.
1.3 Technologies and Services
Through the course of this book, we will used various technologies and external services including:
- Python
- Pip package manager
- Django
- Git
- GitHub
- HTML
- CSS
- JavaScript
- JQuery
- Twitter Bootstrap
- Bing Search API via Azure Datamarket
- PythonAnywhere
Weve selected these technologies and services as they are either fundamental to web development, and/or enable us to provide examples on how to integrate your web application with CSS toolkits like Twitter Bootstrap, external services like those provided by Microsoft Azure and deploy your application quickly and easily with PythonAnywhere.
1.4 Rango: Initial Design and Specification
The focus of this book will be to develop an application called Rango. As we develop this application, it will cover the core components that need to be developed when building any web application. To see a fully functional version of the application, you can visit the How to Tango with Django website.
Design Brief
Your client would like you to create a website called Rango that lets users browse through user-defined categories to access various web pages. In Spanish, the word rango is used to mean a league ranked by quality or a position in a social hierarchy.
- For the main page of the Rango website, your client would like visitors to be able to see:
- the five most viewed pages;
- the five most viewed (or rangoed) categories; and
- some way for visitors to browse or search through categories.
- When a user views a category page, your client would like Rango to display:
- the category name, the number of visits, the number of likes