This book believes in a practical and pragmatic approach of demonstrating Django design patterns and the best practices through examples. For consistency, all our examples will be about building a social network project called SuperBook.
SuperBook focusses exclusively on the niche and often neglected market segment of people with exceptional super powers. You are one of the developers in a team comprised of other developers, web designers, a marketing manager, and a project manager.
The project will be built in the latest version of Python (Version 3.4) and Django (Version 1.7) at the time of writing. Since the choice of Python 3 can be a contentious topic, it deserves a fuller explanation.
While the development of Python 3 started in 2006, its first release, Python 3.0, was released on December 3, 2008. The main reasons for a backward incompatible version were—switching to Unicode for all strings, increased use of iterators, cleanup of deprecated features such as old-style classes, and some new syntactic additions such as the nonlocal
statement.
The reaction to Python 3 in the Django community was rather mixed. Even though the language changes between Version 2 and 3 were small (and over time, reduced), porting the entire Django codebase was a significant migration effort.
On February 13, Django 1.5 became the first version to support Python 3. Developers have clarified that, in future, Django will be written for Python 3 with an aim to be backward compatible to Python 2.
For this book, Python 3 was ideal for the following reasons:
izip
, xrange
, and __unicode__,
with the cleaner and more straightforward zip
, range
, and __str__
.The last point is important. Even if you are using Python 2, this book will serve you fine. Read Appendix A to understand the changes. You will need to make only minimal adjustments to backport the example code.
This section has the installation instructions for the SuperBook project, which contains all the example code used in this book. Do check the project's README file for the latest installation notes. It is recommended that you create a fresh directory, superbook
, first that will contain the virtual environment and the project source code.
Ideally, every Django project should be in its own separate virtual environment. This makes it easy to install, update, and delete packages without affecting other applications. In Python 3.4, using the built-in venv
module is recommended since it also installs pip
by default:
$ python3 -m venv sbenv $ source sbenv/bin/activate $ export PATH="`pwd`/sbenv/local/bin:$PATH"
These commands should work in most Unix-based operating systems. For installation instructions on other operating systems or detailed steps please refer to the README file at the Github repository: https://github.com/DjangoPatternsBook/superbook. In the first line, we are invoking the Python 3.4 executable as python3
; do confirm if this is correct for your operating system and distribution.
The last export command might not be required in some cases. If running pip freeze
lists your system packages rather than being empty, then you will need to enter this line.
Next, clone the example project from GitHub and install the dependencies:
$ git clone https://github.com/DjangoPatternsBook/superbook.git $ cd superbook $ pip install -r requirements.txt
If you would like to take a look at the finished SuperBook website, just run migrate
and start the test server:
$ cd final $ python manage.py migrate $ python manage.py createsuperuser $ python manage.py runserver
In Django 1.7, the migrate
command has superseded the syncdb
command. We also need to explicitly invoke the createsuperuser
command to create a super user so that we can access the admin.
You can navigate to http://127.0.0.1:8000
or the URL indicated in your terminal and feel free to play around with the site.