- Django Design Patterns and Best Practices
- Arun Ravindran
- 740字
- 2021-07-23 19:36:21
SuperBook – your mission, should you choose to accept it
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.
Why Python 3?
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:
- Better syntax: This fixes a lot of ugly syntaxes, such as
izip
,xrange
, and__unicode__,
with the cleaner and more straightforwardzip
,range
, and__str__
. - Sufficient third-party support: Of the top 200 third-party libraries, more than 80 percent have Python 3 support.
- No legacy code: We are creating a new project, rather than dealing with legacy code that needs to support an older version.
- Default in modern platforms: This is already the default Python interpreter in Arch Linux. Ubuntu and Fedora plan to complete the switch in a future release.
- It is easy: From a Django development point of view, there are very few changes, and they can all be learnt in a few minutes.
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.
Starting the project
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.
- Mastering ServiceStack
- Oracle 11g從入門到精通(第2版) (軟件開(kāi)發(fā)視頻大講堂)
- Python入門很簡(jiǎn)單
- 摩登創(chuàng)客:與智能手機(jī)和平板電腦共舞
- Docker進(jìn)階與實(shí)戰(zhàn)
- 基于Java技術(shù)的Web應(yīng)用開(kāi)發(fā)
- Unity Virtual Reality Projects
- Getting Started with SQL Server 2012 Cube Development
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- 從Java到Web程序設(shè)計(jì)教程
- 第一行代碼 C語(yǔ)言(視頻講解版)
- Android項(xiàng)目實(shí)戰(zhàn):手機(jī)安全衛(wèi)士開(kāi)發(fā)案例解析
- Learning Python Data Visualization
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- 深入淺出Python數(shù)據(jù)分析