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:


$ mongod
mongod --help for help and startup options
Fri Sep 27 23:21:33 [initandlisten] MongoDB starting : 
 ↪pid=7991 port=27017 dbpath=/data/db/ 64-bit host=mail
Fri Sep 27 23:21:33 [initandlisten] db version v2.0.6, 
 ↪pdfile version 4.5
Fri Sep 27 23:21:33 [initandlisten] git version: nogitversion
Fri Sep 27 23:21:33 [initandlisten] build info: Linux z6 
 ↪3.8-trunk-amd64 #1 SMP Debian 3.8.3-1~experimental.1 
 ↪x86_64 BOOST_LIB_VERSION=1_49
Fri Sep 27 23:21:33 [initandlisten] options: {}
Fri Sep 27 23:21:33 [initandlisten] exception in initAndListen: 
 ↪10296 dbpath (/data/db/) does not exist, terminating
Fri Sep 27 23:21:33 dbexit: 
Fri Sep 27 23:21:33 [initandlisten] shutdown: going to close 
 ↪listening sockets...
Fri Sep 27 23:21:33 [initandlisten] shutdown: going to 
 ↪flush diaglog...
Fri Sep 27 23:21:33 [initandlisten] shutdown: going to 
 ↪close sockets...
Fri Sep 27 23:21:33 [initandlisten] shutdown: waiting 
 ↪for fs preallocator...
Fri Sep 27 23:21:33 [initandlisten] shutdown: lock for 
 ↪final commit...
Fri Sep 27 23:21:33 [initandlisten] shutdown: final commit...
Fri Sep 27 23:21:33 [initandlisten] shutdown: closing all files...
Fri Sep 27 23:21:33 [initandlisten] closeAllFiles() finished
Fri Sep 27 23:21:33 dbexit: really exiting now

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:


# django-admin version
1.5.1

______________________

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix