Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.


Introduction

This tutorial answers the question, "How do I setup a Django Project from scratch?". Since you're reading this, I assume (err, hope) you know that Django is a Python web framework built for rapid web development. We'll go through the setup, detailing the basic installation procedures of Django and the dependencies required as well as a few additional libraries/extensions to get you started developing ASAP.

We'll also look at a basic workflow you can use as soon as your project structure is setup.

Quick Start

I recommend that you follow these steps in order the first time you start a new Django Project. Each step is modular, so feel free to jump around after the initial setup

  1. Setup: Regardless of the Django version, there is a common set of requirments and dependencies needed before you can get started. We'll detail how to install those requirements/dependencies in this step.
  2. Workflow: In this step we define the basic workflow of setting up a Django Project, meant for use as a quick reference. This is a high level overview.
  3. Django Version: While the last step provide a broad over, this steps gets granular. Pick a specific Django version to learn how to start a Project in that specific version.



    1. Basic Setup

      Regardless of the Django version, please follow this setup process.

      Requirements

      You should have some Python experience and know basic Unix bash commands. If you've never used the command line before, please familiarize yourself with the following commands: pwd, cd, ls, rm, and mkdir.

      For simplicity, all examples use the Unix-style prompt:

      $ python manage.py runserver

      (The dollar sign is not part of the command.)

      Windows equivalent:

      C:\> python manage.py runserver

      Also, in a Unix environment, you may need to add sudo to each command:

      $ sudo python manage.py runserver

      Sudo simply allows regular users to run commands with admin privileges, which modifies the system for all users. You will need to enter your admin password when using sudo.

      What you need for a basic dev environment:

      1. Python
      2. easy_install and pip
      3. Git
      4. Database (SQLite, MySQL, PostgreSQL, MongoDB, etc.)
      5. virtualenv
      6. Text editor (Sublime, vim, Komodo, gedit, etc.)
      7. Django
      8. South (for Django versions 1.5 and 1.6)

      This basic setup will go through the first six steps since they do not depend on your Django version; the setup process will continue after you decide which version you'd like to utilize.

      Note: This tutorial utilizes Python version 2.7.7; however, Python 3 will work as well.

      Python

      Unix environments come pre-installed with Python. To check your Python version, run the command:

      $ python -V
      Python 2.7.7

      If you already have a 2.7.x version, move on to the next step. If not, download and install the latest 2.7.x or 3.x version

      easy_install and pip

      Both easy_install and pip are Python Package Managers, which make it much easier to install and upgrade Python packages (and package dependencies).

      To download easy_install, go to the Python Package Index (PyPI). You need to download setuptools, which includes easy_install. Download the package egg (.egg), then install it directly from the file.

      Pip, meanwhile, is a wrapper that relies on easy_install, so you must have easy_install setup and working first before you can install pip. Once easy_install is setup, run the following command to install pip:

      $ easy_install pip

      Git

      For version control, we'll be using Git. You can check your current version, if you have git already installed, with the following command:

      $ git --version
      git version 1.9.0

      If you do not have a version greater than 1.7.x installed, please download the latest version.

      Database

      SQLite comes pre-installed with Python, and most tutorials utilize SQLite - so let's push ourselves a bit and use MySQL instead.

      First, install MySQL from here.

      Next, start the server, and then setup a new database and a user:

      $ mysql.server start
      $ mysql -u root -p
      Enter password:
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 1
      Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
      mysql>
      mysql> CREATE DATABASE django_db;
      Query OK, 1 row affected (0.01 sec)
      mysql>
      mysql> quit
      Bye

      virtualenv

      It's common practice to use a virtualenv (virtual environment) for your Python projects, which is used to create self-contained development environments (also called "sandboxes").

      Think of virtualenv as a completely isolated container within your computer, where you can install any version of Python and/or libraries and it won't affect anything outside that container. It's like an isolated, soundproof room within your home where you can scream as loud as you want, about anything you want, and nobody else outside that room can hear it.

      NOTE: Besides creating isolated development enviornments, I also use virtualenv to experiment with other versions of Python. I do not want to upgrade my main version of Python, so I have isolated "containers" with specific versions of Python in them to experiment with.

      Install virtualenv with the following command:

      $ pip install virtualenv

      Set up your development structure:

      $ mkdir django_project
      $ cd django_project
      $ virtualenv --no-site-packages env
      $ source env/bin/activate

      You should see (env) before your prompt, (env)$, indicating that you're running within the 'env' virtualenv.

      To exit the virtualenv, type the following command:

      $ deactivate

      Then reactivate when you're ready to work again.

      Text Editor

      Make sure you have some sort of code editor. When in doubt, you cannot go wrong with Submite Text.


      That's it for the basic setup. Take a look at the overall workflow before moving on to the actual Django project setup.

      Cheers!

      workflow

      django15

      django16

      django17

 Back to top