Setting up Python framework Django and no-sql database Cassandra for web development – Step by Step Tutorial

python-cassandra-chapter-1

Setting up environment for God sake

Guys, I will be blunt. I am not teaching 100s of available ways to install Python, Django and Cassandra and to develop applications around it. My development environment is just defined below and everything is practiced around it. Because there are many ways to do things using these languages I have chosen the favorites that made me think is good for beginners to start with.

  • OS: UBUNTU 14.+ or 15.+
  • Python: 2.7
  • Level: Beginners Guide
  • Type: Getting Started
  • IDE: Brackets 1.5 (Experimenting with this one these days)
  • Environment: VirtualEnv 13.1.2
  • Framework: Django 1.8.6
 This content has been updated and tested with the versions defined above. I have been periodically updating the content to match the pace of new releases. 


What you should know before starting?

  1. I won’t tell you why to use any of these technologies. If you have landed here, I assume you have a fair idea of what these technologies do. And you want to learn as a dummy to kick start your first project using it.

Setting up Python, Django and Cassandra

Okay so let’s dive into what you already have in your Ubuntu system and what needs to be installed.

Checking for pre-requisites and installing Python-Django

On Ubuntu, Python is installed by default. As recommended by PEP-394 you can use  python  and  python2  to run Python v2 (2.7) and  python3  to run Python v3 (3.5).

For programmers who are doing development and are not directly setting up the production server should be using the Python installation through  pip  in a  VirtualEnv .

The Python virtualenv package allows you to create self-contained environments for various projects. Using this, you can install Django in a project directory without affecting the primary system.  So every virtual environment will have it’s own project setup and pre-requisites.

# This provides an abstraction of the used apt repositories. It allows you to easily manage your distribution and independent software vendor software sources.
sudo apt-get install python-software-properties
# Update the ubuntu local package index before starting
sudo apt-get update

# Install pip and other important packages
sudo apt-get install python-pip python-dev build-essential libev4 libev-dev libxml2-dev libxslt1-dev
sudo apt-get install libffi-dev
sudo pip install virtualenv virtualenvwrapper
sudo pip install --upgrade pip

# Once pip is installed, you can use it to install the virtualenv package.
sudo pip install virtualenv

# Now, whenever you start a new project, you can create a virtual environment for it.
mkdir ~/ myproject
cd ~/ myproject

# Now, create a virtual environment within the project directory by typing
virtualenv newenv

# To install packages into the isolated environment, you must activate it by typing
source newenv/bin/activate

# In your new environment, you can use pip to install Django. Regardless of whether you are using version 2 or 3 of Python, it should be called just pip when you are in your virtual environment. Also note that you do not need to use sudo since you are installing locally
pip install django

# You can verify the installation by typing
django-admin --version

# To leave your virtual environment, you need to issue the deactivate command from anywhere on the system
deactivate

Creating a Sample Django Project

Now that you have Django installed, You can use the django-admin  command to create a project

#This will create a directory called projectname within your current directory.
django-admin startproject projectname
cd projectname

Installing Cassandra

Cassandra requires that the Oracle Java SE Runtime Environment (JRE) should be installed.

# Add a Personal Package Archives (PPA) using this command to install Oracle JRE package
sudo add-apt-repository ppa:webupd8team/java

# Update the package database:
sudo apt-get update

# Then install the Oracle JRE
sudo apt-get install oracle-java8-set-default

# After installing it, verify that it's now the default JRE:
java -version

# You should see output similar to the following:
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

We’ll install Cassandra using packages from the official Apache Software Foundation repository provided by Datastax, so start by adding the repo so that the packages are available to your system.

# DataStax source for stable version of Cassandra
echo "deb http://debian.datastax.com/community stable main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list

# To avoid package signature warnings during package updates, we need to add three public keys from the Apache Software Foundation associated with the package repositories.
gpg --keyserver pgp.mit.edu --recv-keys F758CE318D77295D
gpg --export --armor F758CE318D77295D | sudo apt-key add -

gpg --keyserver pgp.mit.edu --recv-keys 2B5C1B00
gpg --export --armor 2B5C1B00 | sudo apt-key add -

gpg --keyserver pgp.mit.edu --recv-keys 0353B12C
gpg --export --armor 0353B12C | sudo apt-key add -

#Update the package database once again:
sudo apt-get update

# Finally, install Cassandra
sudo apt-get install cassandra

# If you were able to successfully start Cassandra, check the status of the cluster. If the status is UN, it means it's Up and Normal.
sudo nodetool status

# Then connect to it using its interactive command line interface cqlsh.
cqlsh

Type exit to quit
exit
 django-cassandra-engine  is a database wrapper for Django Framework. It uses the latest cassandra-driver while primarily utilizing Cqlengine which is currently the best Cassandra CQL 3 Object Mapper for Python and was integrated into cassandra-driver. Explore it more on its Official Page.

pip install django-cassandra-engine

Okay so this could be your first Django edit, or may be your first step to Python with Django:

# Add django-cassandra-engine to INSTALLED_APPS in your settings.py where it may look something like this once you have added the code:
INSTALLED_APPS = (
 'django_cassandra_engine',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
)
# Please make sure that this app is the first app on INSTALLED_APPS list. This rule applies only to Django >= 1.7.

# Also change DATABASES setting:
DATABASES = {
  'default': {
    'ENGINE': 'django_cassandra_engine',
    'NAME': 'db',
    'TEST_NAME': 'test_db',
   'HOST': '127.0.0.1',
   'OPTIONS': {
     'replication': {
       'strategy_class': 'SimpleStrategy',
       'replication_factor': 1
     }
   }
 }
}

# Define some model in myapp/models.py
import uuid
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model

class ExampleModel(Model):
    read_repair_chance = 0.05 # optional - defaults to 0.1
    example_id = columns.UUID(primary_key=True, default=uuid.uuid4)
    example_type = columns.Integer(index=True)
    created_at = columns.DateTime()
    description = columns.Text(required=False)

# Sync it with Cassandra DB
python manage.py sync_cassandra

That’s it and here you have a Django application running with Cassandra.

Leave a Reply