Python vs Ruby and Django vs. Rails

In this post I am collecting opinions about pros and cons of using two popular web frameworks Python’s Django and Ruby on Rails in web development. I tried to include all factors that matter to choose between two frameworks.

Python vs Ruby

General

Both languages are good, modern, well-designed, flexible, good for your productivity.

 

Philosophy

– Python really believes that code readability is the most important thing. Hence there is one-true way of writing code.
Python likes things to be structured, consistent, and simple

Ruby believes in giving programmer the flexibility, freedom and power . It provides a better way to write concise and compact code.

More into the expressiveness of the code and writing code that is clever.

 

– Both Ruby and Rails have more ‘magic’ involved. This makes development very fast when you understand the magic, but frustrating when you don’t.

– Django is easier to learn and has less “magic”.

 

Python people like libraries to be transparent and obvious how they work, while Ruby people tend to provide clean and pretty interfaces with “magic” behind the scenes.

 

Convention over configuration

– The centerpiece of Rails’s philosophy is called convention over configuration(CoC). Rails provides you with more defaults.

– Django on the other hand let’s you specify most configuration details yourself.

 

Application

– Python is more mature, general purpose nature, vs Ruby’s more niche (Rails) usage.

– Ruby+Rails holds a slight edge over Python+Django for web development, and has more mindshare.

– Python is stronger for things like data manipulation, analytics, system administration, scientific programming,  etc.

 

Community

– There are two great communities behind both frameworks, which bring you plugins, extensions and so on.

Some people believe that Python has a more developed community. Python community has developed tons of libraries suited for data analysis, machine learning, natural language processing, scientific libraries.

But the number of gems and extenstions to Rails is growing fast.

 

 

It’s more a choice you’ll made between their philosophies than their (similar) features set.

 

 

Django vs. Rails (with ActiveAdmin or RailsAdmin)

Ruby on Rails and Python (Django) are two leading frameworks.

Both are great MVC frameworks on top of a great language.

 

General

Django is more declarative you’ll have a clearer understanding of what’s actually going on.
Rails prefers convention over configuration.

 

First application

Compared to Rails, Django creates a much simpler project structure.

 

Rails features

Convention over configuration (CoC)
CoC in Rails basically means that a Rails project has a predefined layout. All components (models, views, controllers, layouts, css, javascript, etc) have standard places where you should drop them and the application picks them up without any additional effort on your part.

DRY

Another centerpiece in Rails is the “Don’t Repeat Yourself”(DRY) principle.

 

MVC

Rails is a classical Model-View-Controller full stack web framework.
What separates it from most of the MVC frameworks around is the heavy emphasis on REST.

Model contains the application’s business logic, Controller invokes functionality from the model layer and feeds the resulting data to View, which display the data in a readable way to the user.

ORM
The model layer is the home of your domain objects and the business logic surrounding them.
Domain objects (a.k.a. entities) are mapped to database tables (at least in RDBMS).
Unlike most object relational mappers Rails’s ActiveRecord (the default ORM; could be substituted with something else if you wish) doesn’t require you to explicitly declare the structure of the objects, but rather extracts it automatically from your DB table definitions. A simple class, modeling an application user might look like this:

– Finders – Rails generates automatically for us “finders” that we can use to make queries about objects in pure Ruby.

– DSL to express relationships between various model classes (belongs_to, has_many, etc)

 

Views and HTML Templates

In the view layer Rails relies on HTML templates with Ruby code embedded in them (html.erb).

There are a lot of community contributed alternatives, however, with HAML being the most prominent one.

Rails allows you to use SASS as a replacement for traditional CSS.

Rails provides the following tools to be used in views to follow its philosophy (DRY and CoC): partial templats, layout, helpers.

 

Ajax and Rails

Rails features tight integration with JavaScript and AJAX. jQuery is the default JavaScript library.

Making and handling AJAX requests in Rails is generally very easy.

 

Testing 

Rails heavily promotes testing your code. All of the Rails generators would generate test stubs, that you’ll do good to fill in.

Rails supports all major Ruby test frameworks – Test::Unit, RSpec, Cucumber.

Features:

– fixtures support,

– test database support,

– the ability to run unit and functional test separately.

Rails really pays much attention to having tests.

 

Deployment

The most common deployment options for Rails are currently Apache HTTPD or NginX and Phusion Passenger (mod_rails).

 

 

Django features

 

MVC

Django calls its separation of domains MTV (model-template-view) instead of the traditional MVC. It uses templates instead of Rail’s views.

While Django is a MVC controller framework as well, it’s built around a different mindset.

Django is totally configurable and minimalistic framework that empowers the developers to tailor it to their needs.

A Django project is comprised of autonomous and reusable apps. Apps on the other hand are comprised of models, views and templates.

 

ORM

Django’s default ORM is somewhat reminiscent of frameworks like Hibernate. Entity classes declare explicitly the all the attributes. So you have to write more code than you do in Rails.

Like in Rails we get useful “finders” that can be used to query for model objects in pure python (as opposed to using sql).

 

View (template layer)

While ERB is basically HTML with Ruby embedded in it, Django features a custom templating language with it’s own (extendable) tag library. This generally means that Django templates tend to be a bit cleaner than Rails’s templates, since you’re not allowed to abuse them very much.

On the other hand you can do virtually anything by embedding code directly in a template, so as usual – each design decision has its pros and cons.
Django supports alternative templating libraries, so you’re covered in case you don’t like the default one.

 

Form generation

Django features a powerful form generation/handling facilities that integrate seamlessly with the templating layer. For instance – it very easy to generate a form matching the structure of a domain model object with automatic property validation.

Rails also has gems for helping generating forms (Formtastic gem) and client-side validations.

 

Javascript and Ajax

Unlike Rails, Django doesn’t bundle any JavaScript libraries. You have to pick a JavaScript framework yourself (which a trivial process).

The AJAX support in Django is both basic and extremely powerful in the same time – request.is_ajax(). What this means is that you can check if a request was regular or an AJAX request and respond accordingly.

 

Testing

– Environments
This is a feature that comes out of the box in Rails and doesn’t in Django. Django is explicit, and you can make your environment system.

 

 

Deployment

Python is quite mature technology and offers you many deployment options – Apache, Nginx and Google App Engine.
The also the possibility to deploy Django apps on Java or .Net infrastructure using Jython or IronPython.

 

 

Admin area

Django comes with authentication and extensible admin built in. It makes it easy to build standard pages for admin to manage your data.

It makes development of simple CMSs really easy.

It is one of the Django advantages – the automatic admin interface you get for free out-of-the-box.

Rails doesn’thave a built-in support of admin. But you have a choice to use several available extenstions (gems). Currently the two most populars admin UI ‘frameworks’’ are RailsAdmin and ActiveAdmin.

 

 

Read more about comparison of RailsAdmin with ActiveAdmin:

http://batsov.com/articles/2011/11/20/admin-interfaces-for-rails-apps-railsadmin-vs-activeadmin/

http://stackoverflow.com/questions/6542075/rails-admin-vs-activeadmin

 

References:

– Discussion on stackoverflow: http://stackoverflow.com/questions/91846/rails-or-django-or-something-else

http://batsov.com/articles/2011/06/19/django-vs-rails/

RAILS VS DJANGO

Which should I learn: Django or Rails? (Discussion on Quora)

Do You Believe In Magic?, written in 2009

 

  • Cristiano Politowski

    Great article. Helped me alot.

  • Bhavana chavhan

    Nice article and comparision between ruby
    on rails outsourcing
    and python.