Book Image

Instant Django 1.5 Application Development Starter

By : Mauro Rocco
Book Image

Instant Django 1.5 Application Development Starter

By: Mauro Rocco

Overview of this book

<p>Django is a high-level Python application framework built for development speed and accuracy. With a flatter learning curve than most frameworks, Django quickly enables you to create elegant, performant applications.<br /><br />In this book you will jump into Django by creating a web application step by step. Offering you a quick but comprehensive immersion into the world of Python development, Instant Django 1.5 Application Development Starter begins with a practical exploration of the framework’s powerful mechanisms and tools and finishes your journey by taking you through the creation of one sample application.<br /><br />In "Instant Django 1.5 Application Development Starter" you will learn how Django’s components work together. You will get familiar with the powerful template system, and will discover how easy it is to generate a web administration interface for your project. You will learn how to work with user authentication, forms, and session management.<br /><br />Through this book you will learn everything you need to know to create stunning web applications in a very short time, taking advantage of all the beauty and power that Python has to offer. If you want to start building serious web applications using Python, this book is all you need.</p>
Table of Contents (7 chapters)

Quick start – your first Django project


Let's start from the basics. Django is based on projects, so the first thing we will do is to create one and then test it on our browser.

Step 1 – Create your first project

To get started with Django the first thing we need to do is to create a project.

A project is just a collection of settings organized in a defined folder structure to run an instance of Django and they can be autogenerated using a Django-admin command offered by the Django package.

Note

If you are using Windows you will need to add the path of the Script folder containing the Django-admin command (C:\Python27\Script) to the environment variables.

From the command line, move into a directory where you would like to store your project and run the following command:

django-admin.py startproject jumpingintodjango

This will create a jumpingintodjango directory in your current directory.

Note

After the autogeneration of the project you should avoid renamimg the folders inside the project directory using names of built-in Python or Django components. This means that you should not use names like "django" or "test".

Now let's take a look at the directory structure that we just created:

jumpingintodjango/
  manage.py
  jumpingintodjango/
    __init__.py
    settings.py
    urls.py
    wsgi.py

Let's understand what these files are used for:

  • The root directory jumpingintodjango/ is just a container for the project, and its name is not used by Django. Feel free to rename it without any constraints.

  • manage.py is a command line utility that lets you operate on your Django project in many different ways.

  • The inner jumpingintodjango/ directory is the Python package for your project.

  • If you are a Python developer you already know that jumpingintodjango/__init__.py is an empty file that tells Python that this folder needs to be considered as the Python package.

  • jumpingintodjango/settings.py is for the settings/configurations for this Django project.

  • jumpingintodjango/urls.py is the URL configuration for this Django project. It is a clean and elegant URL scheme without any framework limitation.

  • jumpingintodjango/wsgy.py is the file that will be used as an entry point when you deploy this Django project to a web server that is WSGI-compatible.

Step 2 – The Django development server

If you already have experience in the development of websites with other languages like PHP, you may notice that you need a web server installed on your machine, like Apache, to run your code.

With Django you don't need that, in fact it is shipped with a built-in lightweight web server written purely in Python.

Let's run it to check if our project is able to run.

Navigate to the outer directory jumpigintodjango/ and run the following command:

python manage.py runserver

You will see the following output on the command line:

Validating models...

0 errors found
Django version 1.5, using settings 'jumpingintodjango.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C

Note

This server has to be used only for development purposes. It has not been built to be a web server to be used in production environments.

Now that the server is running, you can go to http://localhost:8000/ with your preferred web broswer. You will see a screen like the following:

The server runs by default on port 8000 and on the default local host IP 127.0.0.1. You can change this by passing the port as the first argument of the runserver command, or if you like you can also change the IP to the complete uri:port string like in the following example:

python manage.py runserver 0.0.0.0:8000

This command will expose the web server on all public IPs of your computer; this will allow you to expose the web project to other people.