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.
You can verify it exists, and its location, by typing ‘which python’ in the Linux terminal.
~ > which python /usr/bin/python
You can already execute Python code to print ‘Hello World!’
In a Linux console, type ‘python’.
~ > python
You’ll see three greater-than symbols. This is Python’s new entry line indicator. Type “print ‘Hello World!'” without the double quotes and hit enter. The output will appear directly below the entry line without the line indicator symbols.
>>> print 'Hello World!' Hello World! >>>
That was easy! While we’re here, we should verify our Python version. We’ll import the sys package, and use a couple of functions contained in sys.
>>> import sys >>> sys.version >>> sys.version_info
Step 2: Install the Python Package Manager: pip
Pip stands for Pip Installs Python, which is a bit meta. Who is pip anyway? Is it short for Pippin, a la Lord of the Rings? Regardless, it allows us to more easily install packages right to the specified Python virtual environment (more on virtual environments in the next post).
~ > sudo apt-get install python-pip
Step 3: Install Django
~ > sudo pip install Django
Pay attention to the difference in the install statement above. Here we used ‘pip’ in place of the standard ‘apt-get’ to install the Django package.
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) >>>
Great! 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: “django-admin startproject mysite”. 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 ~/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:
~/mysite > 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! >>>
Congratulations! You’ve successfully set up an environment running Django for Python!
“That’s great,” I hear you say, “but what do I DO with Django?” Well, in my next post, I’ll be showing you how to set up a quick local web page.