Running Django application on Vagrant

Vagrant is useful to create and configure lightweight, reproducible, and portable development environments.

Here I am going to show you how to setup Vagrant on Ubuntu 14.04 and how to up and running a Django application on it.(You can try same with Rails application)

Follow these steps.

1. Install Virtualbox:
    $ sudo apt-get install virtualbox

2.  Install Vagrant
    $ sudo apt-get install vagrant

3. Preparing Django application for VM:

Go to application home directory and Initializes the it to be a Vagrant environment aware.
    $ vagrant init

This will create an initial Vagrantfile if one doesn’t already exist. You might have to override some of the default configurations. Here is the whole config file from my local setup.

# -*- mode: ruby -*-
# vi: set ft=ruby : do |config|
# Base box to build off, and download URL for when it doesn’t exist on the user’s system already = “ubuntu/trusty32”
config.vm.box_url = “”

# As an alternative to precise32, VMs can be built from the ‘django-base’ box as defined at
# , which has more of the necessary server config
# baked in and thus takes less time to initialise. To go down this route, you will need to build
# and host yourself, and substitute your own URL below. = “django-base-v2.2”
#config.vm.box_url = “” # Torchbox-internal URL to

# Boot with a GUI so you can see the screen. (Default is headless)
# config.vm.boot_mode = :gui

# Assign this VM to a host only network IP, allowing you to access it
# via the IP.
# “”

# Forward a port from the guest to the host, which allows for outside
# computers to access the VM, whereas host only networking does not.
config.vm.forward_port 8000, 9000

# Share an additional folder to the guest VM. The first argument is
# an identifier, the second is the path on the guest to mount the
# folder, and the third is the path on the host to the actual folder.
config.vm.share_folder “project”, “/home/vagrant/dfportal_shared”, “.”

# Enable provisioning with a shell script.
# config.vm.provision :shell, :path => “etc/install/”, :args => “myproject”

4. Start Vagrant
    $ vagrant up

5. After successful login, go to root directory and run following command:
     $ cd home/vagrant/mydjango_apps_shared

You should be able to find there.

The mydjango_apps_shared is the shared directory between your machine and VirtualMachine. Quite Amazing!!

Install required packages for Django Application:

6. Install python package installer on VM

    $ sudo apt-get install python-pip

7. Install Django using pip:

     $ sudo pip install django

8. Install required packages:

    $ sudo pip install -r requirements.txt

9. Setup Mysql DB in VM

    $ sudo apt-get install mysql-server

10. Then connect to mysql and create a DB for your application
   $ mysql -u root -p
   Enter password:
      > create database my_db
      $ exit

11. Synchronize the database state with the current set of models and migrations
        $ python syncdb

12. To load fixtures run
     $ ./ loadfixtures

13. Up and Running
    $ python runserver

Now start the browser and hit localhost:9000 or and your application works!!!

If you can’t see the page, just look back to Vagrant file and check config.vm.forward_port settings to exactly know which port the VM is listening and forwarding.


Importance of writing Test cases in an existing Rails Application

Hey guys!!

In my first article, I going to introduce you the importance of writing test cases(either with Rspec or minitest) with existing Rails Application and why one should write it?

Once you have created your Rails application and now you are quite excited with your bunch of code, files and directories. The only thing that you are missing now is Test cases??

Actually It’s not TDD(Test Driven Development) but It’s the way to learn TDD.

There are three important benefits by writing test cases:

1. You practice about thinking all cases and all possible conditions.
After writing enough tests, you will become aware of where is the possibility of breaking of methods, And as soon as you start TDD you can use this skill  to write robust tests to handle all possible cases of your application.

2. You get habituated with well structured tests.
Once you have done enough practice for writing tests, now you can apply different design patterns for structuring those tests.

3. You find the tests which are hard to  test.
As soon as you get more familiar, you will understand that which part of application is very hard to test and which part of the application is very easy to test. This will provide you the strength of analysis for future estimation of any project.

4. Tests are also very important while migration.
If your rails application has been written all possible test scenarios, then you can upgrade your rails application easily. You can upgrade it to any latest stable version.

Ease into TDD

Whenever you are testing your application,instead of clicking around in the browser to reproduce it, it better two write some cases.

Here is the flow of writing test cases:
a. Write a failing test
b. Run the test,
c. Fix the bug
d. Run the tests
e. Refactor tests

TDD is always preferable to write new rails application.(if you have enough time)

In my next post I will show you how to write tests? and how to pass them.

Have A Happy Coding!!!