Django Hello World! A Framework for Python Web Apps

Django Hello World! A Framework for Python Web Apps

First up, Hello World in Django, from OS install to runtime!

If you haven’t already, check out my previous post How to Install Linux Mint. I’ll be setting up the Django development environment from a fresh install of Linux Mint.

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.

You can already execute Python code to print ‘Hello World!’

In a Linux console, type ‘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.

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.

python_hello_and_version

 

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).

 

Step 3: 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.

django_version

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’.

start_project_marked

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

manage_pyYou 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:

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.

hello_world_django_shellCongratulations! 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.

Stay tuned!

One thought on “Django Hello World! A Framework for Python Web Apps

Join the Discussion

%d bloggers like this: