Skip to content
Christopher Hopkins edited this page Apr 14, 2014 · 5 revisions

Description

From Wikipedia:

Django is a free and open source web application framework, written in Python, which follows the model–view–controller architectural pattern. It is maintained by the Django Software Foundation (DSF), an independent organization established as a 501(c)(3) non-profit.

Django's primary goal is to ease the creation of complex, database-driven websites. Django emphasizes reusability and "pluggability" of components, rapid development, and the principle of don't repeat yourself. Python is used throughout, even for settings, files, and data models. Django also provides an optional administrative create, read, update and delete interface that is generated dynamically through introspection and configured via admin models.

Some well known sites that use Django include Pinterest, Instagram, Mozilla, The Washington Times, and the Public Broadcasting Service

Requirements

From the project website:

Being a Python Web framework, Django requires Python. It works with Python 2.6, 2.7, 3.2 or 3.3. All these versions of Python include a lightweight database called SQLite so you won’t need to set up a database just yet.

Install PIP

PIP is a Python Software Package Manager. You can use it to install Django.

Download the installation script from github:

$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py > get-pip.py

Run the installation script:

$ sudo python get-pip.py

Install Django

$ sudo pip install Django

Verify Installation

$ python
PYTHON Version Information
>>> import django
>>> print(django.get_version())
>>> exit()

Create a new Django Project

$ django_admin.py startproject <project_name>

This will cause the following directory structure and files to be created:

 project_name/
    manage.py
    project_name/
        __init__.py
        settings.py
        urls.py
        wsgi.py
  • manage.py is a script which you will use to perform many tasks
  • project_name/ subfolder contains important project files
  • init.py is an empty file which tells python that this directory should be treated as a python package
  • settings.py is the project configuration file
  • urls.py routes urls to scripts
  • wsgi.py enables the project to interface/operate as a WSGI application

Running the Project in a development webserver

$ python manage.py runserver (optional IP Address):(optional port)

You can use this webserver to view your progress and test the functionality of your project.

Note: Django provides an administration framework by default. In order to use it, you must first run 'syncdb' to create the database, and initial admin user:

$ python manage.py syncdb

Creating Applications

While you have created the project, you still haven't done anything useful yet. The project provides the initial framework, but you should create 'application(s)' inside your project to do useful things.

To create an application do the following:

$ python manage.py startapp <app_name>

This will create the following directory structure and files inside your main project directory:

app_name/
    __init__.py
    admin.py
    models.py
    tests.py
    views.py
  • init.py performs the same function as it does in the project subfolder
  • admin.py is used to define how the application is administered by the built-in administration framework provided by Django
  • models.py is where you will find the classes which define the importation data structure for the application
  • tests.py is where you will create tests for your project
  • views.py

Django Python Shell Environment

You can interact with your Django environment through a python shell.

Use the following command to enter the shell:

$ python manage.py shell

You will now enter the familiar '>>>' python shell and be able to import classes from your Django project and interact will them. This environment can be an invaluable resource to troubleshoot your project and applications.


Resources

Django Documentation and Tutorials (Version 1.6)

Clone this wiki locally