POSTS

Django, A Framework for Python Web Apps

This tutorial is intended as a quick setup guide, designed to get you started as quickly as possible. I’ve found the best way to learn is to jump in and get your hands dirty. Then when you read the documentation, there’s context  behind it, and it sticks a little better. While I’ll include basic explanations in this post, you should definitely read through the Django tutorials if you’re interested in learning more about the individual commands and design elements.

What is Django, Anyway?

Django was designed to simplify the web page building process (or more accurately, the blog building process), and compartmentalize all the necessary components for a webpage in a very structured, template-based framework. This framework utilizes what is commonly known as the Model-View-Controller (MVC) design pattern. The Model object holds the data, the View object displays the data, and the Controller connects the Model and the View together, controlling the data communicated between them. As a developer, you take these pieces and wire them together, overriding elements where customization is needed. We’ll go more in-depth with the individual components of a Django project in the next post.

Step 1: Confirm Python Installation

Python comes pre-installed with most Linux distributions.

If you’re interested in installing Python 3, here’s a handy installation tutorial and here’s a Python 3 starter guide, and another one. Here’s another install guide, though it’s a little older.

You can verify it exists, and its location, by typing ‘which python’ in the Linux terminal.

which python
/usr/bin/python

Bring up the python console and execute a print 'hello world':

python
...
>>> print 'Hello World!'
Hello World!
>>>

Step 2: Install the Python Package Manager: pip

sudo apt-get install python-pip

Step 3: Install Django

sudo pip install Django

Note: you can check your Django version through a Python console in the Linux terminal by typing the following 3 commands.

python
>>> import django
>>> django.VERSION
(1, 9, 2, 'final', 0)
>>>

Now that we’ve installed pip and Django, we’re ready to start our first Django project.

Step 4: Start a Django Project

To start a Django project, type the following in the Linux terminal.  This will create a new directory in your current location called mysite.

django-admin startproject mysite

Now, move to the mysite directory, and check out the files located there.

cd mysite
ls

You should see a Python file named manage.py. This file is the workhorse of your application. It handles a variety of tasks, such as creating and applying your database scripts, running your local web server, or running the Django shell.

Guess what! At this point you can get into the Django shell in your new project and output Hello World!

Step 5: Text Output From the Python Django Shell

Let’s access the shell now:

python manage.py shell

You’ll see 3 greater-than characters (just like the Python console!) This may look exactly like the Python console, and that’s because for the most part it is. The difference is that Python manage.py shell will point to instance of Python in the project’s virtual environment files (if you have them set up, which we will cover in the next post.) ‘Hello World!’ will work exactly the same as the Python console example above.

>>> print 'Hello World!'
Hello World!
>>>

Visit More Nuts and Bolts of a Django App for a sample web app tutorial.