Using Django and MongoDB to Build a Blog

This article shows how to create a simple blog site using the
MongoDB Document Database and the Django Web framework.

Mongo Basics

MongoDB is an open-source document-oriented database, not a traditional
relational database, written in C++ by Dwight Merriman and Eliot
Horowitz. Being a document database does not mean storing Microsoft Word
documents, but rather it means storing semi-structured data. You can input arbitrary
binary JSON objects (BSON) into a MongoDB database. It runs on UNIX
machines as well as Windows and supports replication and sharding.

Your Linux distribution probably includes a MongoDB package, so go
ahead an install it if you have not done so already. Alternatively,
you can download a precompiled binary or get the MongoDB source code
from http://www.mongodb.org and compile it yourself.

On a Debian 7 system, you can install MongoDB with the following command:

# apt-get install mongodb

After installing MongoDB, start the MongoDB server process with:

# service mongodb start

Similarly, you can stop the running MongoDB server with:

# service mongodb stop

After installation, type mongo --version in your
UNIX shell to find the MongoDB version you are using, and type
mongo to enter the MongoDB shell and check whether the
MongoDB server process is running.

By default, the MongoDB server process listens to localhost using the 27017
port. You can change it if you want, but if both the MongoDB server and the
Django installation are on the same machine, it is more secure to leave it
as it is.

The configuration file for MongoDB is /etc/mongodb.conf. Nevertheless, if
you want to run multiple MongoDB servers on the same UNIX machine, you can
bypass the /etc/mongodb.conf file and utilize command-line options that
allow you to use a different port number, a different IP or even a
different MongoDB configuration file.

Figure 1. MongoDB Terminology

Figure 1 shows the most useful MongoDB terms in relation to their
respective SQL terms.

Starting the MongoDB server process (mongod) on a Linux machine without any
parameters and without root privileges should generate output similar to
the following:

Django Basics

Django is a high-level Python Web framework that encourages rapid
development and clean, pragmatic design. It allows you to build Web
applications quickly. Instagram, Mozilla and Pinterest use Django.

Simply put, Django is a collection of libraries written in Python. In order
to create a site using Django, you basically write Python code that uses
the Django libraries. If you already have a good working knowledge of Python,
you have to understand only how the Django libraries work.

Django follows a slightly changed version of the MVC (Model View
Controller) design pattern called Model Template View (MTV). The MTV
handles the Controller work by the core, and all the other work is done in
Models, Templates and Views. According to Django's philosophy, what is
truly important is not terminology but getting things done.

On a Debian 7 system, you can install Django with the following command:

# apt-get install python-django

To make sure that everything works as expected, type the following Django
command, which prints the version of Django: