Over a million developers have joined DZone.

Getting Started: Django and NuoDB

· Database Zone

Sign up for the Couchbase Community Newsletter to stay ahead of the curve on the latest NoSQL news, events, and webinars. Brought to you in partnership with Couchbase.

Curator's Note: The content of this article was written by Luke Bayas over at the NuoDB techblog. 

Web development frameworks are the backbone of today’s scale-out database applications. They come in many different flavors, some are minimalistic providing more leeway with various components while others are more fully featured with a tightly integrated stack. At the end of the day, all these frameworks aim to do the same thing: make it easier and faster for developers to create web applications that solve problems.

The Python community has a variety of web frameworks, the clear front runner by far is Django, a full featured framework designed to make it extremely simple to develop robust web applications. Django has a huge community with a number of useful 3rd party apps. I personally recommend the Django framework as a means to implement your next killer web app and the community for their responsiveness to any questions or issues that may arise.

To this end, we are proud to announce the release of the NuoDB extension for Djangoto enable your web apps to leverage the power of NuoDB’s scale-out architecture with the ease of Django’s web application framework.

In this article we are going to talk more about Django and NuoDB while covering the following topics:

  1. Installation
  2. Django Sample Application
  3. Settings Configuration
  4. Admin Interface Configuration (optional)

Introduction

The modern web landscape has seen a surge in mobile-based applications, whether they are native or web-based, alongside traditional web applications. Inherent in the web and mobile app architectures is a common need to connect an application server, typically through a CRUD interface, to retrieve or manipulate data. On the backend, it is common practice to use a web app framework as a starting point to get up and running quickly to get your app out there. This post walks you through creating a simple Django app leveraging NuoDB so you can get started building your next killer application quickly.

Let’s get started …

Part 1 – Installation

Before we dive into implementing a NuoDB based web app using Django, we need to install the required packages, Django, the NuoDB Python driver and the NuoDB extension. Django can be installed in a variety of different ways, the easiest and most manageable way is to install the package with pip, a python package management tool similar to npm for node.js and gem for ruby.

Let’s install the latest version of Django …

sudo pip install django

Next, let’s install the NuoDB python driver (extension leverages this to talk to NuoDB)

sudo pip install pynuodb

Similarly, let’s do the same for django_pynuodb

sudo pip install django_pynuodb

Alternatively, you can clone the github NuoDB extension repo and install it

git clone https://github.com/nuodb/nuodb-django
cd nuodb-django
make install

It’s that easy. So we are all set installing Django, our web framework of choice, the NuoDB Python driver, our native interface to NuoDB and django_pynuodb, our extension to leverages the driver to talk to NuoDB.

Let’s create a sample Django app …

Part 2 – Django Sample Application

Similar to other frameworks, Django makes it easy to create a sample application with boilerplate to get off the ground fast.

django-admin.py startproject nuodb_site

In this directory you will find the following boilerplate code

nuodb_site
	__init__.py
	manage.py
	nuodb_site
		__init__.py
		settings.py
		urls.py
		wsgi.py

This is the base from which many awesome Django app’s are built. Most of the files are self explanatory, the one that we care about specifically is settings.py which contains all the configuration you will need to setup your app.

Let’s update settings.py to leverage the django-pynuodb extension we installed in Part 1 of this post …

Part 3 – Settings Configuration

The heart of web apps rests in the database as it serves as the resource vast amounts of data are stored, queried and interpolated in a variety of different ways. The challenge many web apps face today is how to efficiently scale their database as their web app scales. It is common practice to shard one’s database to increase capacity. Thus, it is easy to manage scaling a web app horizontally in the cloud (via adding more application instances), however, it is much more difficult to manage scaling databases in the same environment. NuoDB is an absolutely fantastic pairing to web apps as it was built to run in the cloud and can scale as an application scales extremely easily. No sharding required. Awesome!

Let’s setup Django to leverage the django_pynuodb extension to talk to NuoDB.

Whether you are starting a new project or using an existing one, we need to modify the settings.py DATABASES dictionary to use our extension instead of the default

DATABASES = {
    'default': {
        'ENGINE': 'django_pynuodb',                                                                          
        'NAME': 'test',                                                                                                                                                                                                                        
        'USER': 'dba',
        'SCHEMA': 'django',
        'PASSWORD': 'secret',
        'HOST': 'localhost',                                                               
        'PORT': '48004',                                                                                                                          
    }
}

In the above, the ENGINE references our django_pynuodb package, the remainder of the items are typical database items. Additionally, make sure to set USE_TZ to False in settings.py. The timezone is determined when the connection is initially made to NuoDB and persists throughout the life of the connection.

NOTE, the SCHEMA dictionary key is optional and defaults to the USER schema. Supplying a schema name here will populate the schema with Django and user defined tables.

At this point, we have everything setup, so let’s do a syncdb to update our database. Note, you will be asked for a username and password to create the initial admin dba user for the database, make note of these credentials

python manage.py syncdb

If you experience any errors when performing the sync, double check thatdjango_pynuodb is in your PYTHONPATH.

At this point, all django specific models as well as user defined models are persisted in NuoDB. If you launch nuosql you can see all django specific tables under thedjango schema (defined above).

Launch nuosql to connect to your database

nuosql test@localhost --user dba --password secret

And view the tables under the django schema

SQL> show tables in django

	Tables in schema DJANGO

		AUTH_GROUP
		AUTH_GROUP_PERMISSIONS
		AUTH_PERMISSION
		AUTH_USER
		AUTH_USER_GROUPS
		AUTH_USER_USER_PERMISSIONS
		DJANGO_ADMIN_LOG
		DJANGO_CONTENT_TYPE
		DJANGO_SESSION
		DJANGO_SITE
		POLLS_CHOICE
		POLLS_POLL
SQL>

We now have Django specific tables under our django schema and can now start implementing our web application.

Part 4 – Admin Interface Configuration (optional)

One of the great tools that Django has built in an admin interface that allows one to view and manipulate their database. For those who are not as comfortable with databases, this is a great way to interact with it without having to explicitly deal with SQL.

Let’s set it up …

Uncomment the following line in settings.py:

'django.contrib.admin'

Uncomment the following 3 lines in urls.py:

from django.contrib import admin                                               
admin.autodiscover()

url(r'^admin/', include(admin.site.urls))

Perform another syncdb to persist any admin related updates to NuoDB

python manage.py syncdb

Congrats! You have successfully setup Django & NuoDB to interface with each other.

Do the following to run the server

python manage.py runserver 8000

Now launch your browser and go to localhost:8000/admin

You should see a sign in screen. Enter in the credentials from earlier and you are now off and running. From here I encourage you to walk through the Django tutorial, it is really good and will cover in detail how to setup your models for business objects you want to capture in NuoDB and how to setup routes to templates.

Summary

In this blog post I’ve outlined the installation process of Django and the NuoDB Extension, how to configure Django to interface with NuoDB and how to launch the NuoDB admin interface.  For more detailed instructions how to use Django, I recommend that you work through the Django tutorials. They are very thorough and walk you through every aspect of the framework you will need to create robust applications.

For those interested in understanding NuoDB on a more technical level, I highly recommend checking out Seth Proctor’s blog posts

Happy coding!








Are you a developer? Don’t miss the monthly Couchbase Developer Community Newsletter, covering the latest headlines on NoSQL and much more. Brought to you in partnership with Couchbase.

Topics:

Published at DZone with permission of Seth Proctor, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}