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
Both languages are good, modern, well-designed, flexible, good for your productivity.
- 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.
- 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.
- 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.
Django is more declarative you’ll have a clearer understanding of what’s actually going on.
Rails prefers convention over configuration.
Compared to Rails, Django creates a much simpler project structure.
Convention over configuration (CoC)
Another centerpiece in Rails is the “Don’t Repeat Yourself”(DRY) principle.
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.
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
Making and handling AJAX requests in Rails is generally very easy.
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.
- fixtures support,
- test database support,
- the ability to run unit and functional test separately.
Rails really pays much attention to having tests.
The most common deployment options for Rails are currently Apache HTTPD or NginX and Phusion Passenger (mod_rails).
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.
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.
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.
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.
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.
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.
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:
- Discussion on stackoverflow: http://stackoverflow.com/questions/91846/rails-or-django-or-something-else
- Do You Believe In Magic?, written in 2009