First Steps in Django from Rails guy perspective (part 1)

Photo of Jacek Jakubik

Jacek Jakubik

Updated Feb 27, 2024 • 9 min read

I am a Rails developer at Netguru, and every now and then, I get interested in other languages or frameworks that we can use to build web apps.

Just recently, I experimented with node & express, but now I’d like to try some Python (and Django) and walk you through the process of what I experience as a Rails developer learning Django.

I am a Rails developer at Netguru, and every now and then, I get interested in other languages or frameworks that we can use to build web apps. Just recently, I experimented with node & express, but now I’d like to try some Python (and Django) and walk you through the process of what I experience as a Rails developer learning Django.

Keep in mind I’ve never written anything in Python before, and I only briefly glimpsed Django, so this will be a raw first impression. However, since I’m a Ruby / Rails developer, I expect to see some similarities between the two (which is probably terrible and will lead me astray).

Anyway, let’s get down to write a Django app! I’ll try to create a simple blog application which has posts that can be commented upon. In Rails, it’s about 10-15 minutes of work.

Version Manager

So Ruby has RVM, which is the first search result in Google with the ‘ruby version manager’ phrase, so I’d expect to see something similar for Python.

Searching for ‘python version manager’, however, shows me... stackoverflow - Is there a python version manager?!. After another quick search I found virtualenv, so I thought I’d give it a shot. The installation is via… pip install virtualenv, where the pip command comes from python, so it required me to have any python already installed on system to install it’s environment manager. According to virtualenv, I have to have at least pip 1.3 and I have 1.4, so we’re fine.

Let's install it:

Ok, that went well, so we’ve got a python manager installed. It looks like we don’t have to add any additional lines to our .bashrc or .zsh files in order to load it up.

I already have python 2.7 installed on my mac, so I guess I don’t even need this version manager, at least now, since I’ll be using this version in my Blog anyway.

Installing Django

In order to create a new Rails app, you need to install a gem called ‘rails’ and then execute a command to bootstrap an empty skeleton of the app. Let’s find out how to do that in Python/Django.

It looks like Django has a nice tutorial on their site: Tutorial01

I’ll use this one here then. They say that the tutorial is written for Python 2.x, so I guess we’re fine at this point.

They assume I have Django already installed, and by running

python -c "import django; print(django.get_version())" it will show me the version. We do see this:

Ok so it looks like I don’t have Django... Let's install it, using this: Django project install

Install Django

So I already have that, I can skip this part.

  • Install Apache and mod_wsgi

    I don’t need that, since I’m only experimenting. Skip.

  • Get your database running

    I will need a database, let’s say I’ll use mysql for this blog. So it says I have to install the MySQL-python package… but how do I install it? Via pip? I’m not sure, let’s try it:

  • Ok so it did something, some errors / warnings came out, but in the end, I successfully installed MySQL-python. Well, at that point pip command works the same way as gem command for installing ruby gems. So I guess installing Django will be as easy as mysql package? We’ll see about that…

  • Remove any old versions of Django

    I don’t have any versions of Django, so I’ll skip this part

  • Install the Django code

    So it looks like pip install Django should do the case, let’s see how it goes:

It looks like something is wrong with my installation, since I never installed Django on this machine, let’s do what they ask me to:

I used sudo because my mac was crying about the permission when I ran this command without sudo the first time.

  • Ok! so it looks like we’ve got Django installed, let’s check it:
<code>Still nothing, let’s see the paths that python is looking the packages for: </code>
<code>This array does not include the directory where my Django has been installed (which is /usr/local/lib/python2.7/site-packages, why this dir? No idea). After a quick search it looks like Python is looking for paths using PYTHONPATH environment variable, which I have modify in .bashrc or similar place to add a path. </code>
  • Ok, I’ve added the variable, let’s see if it works:
<code>Great, so we’ve got 1.5.4 Django installed. Nothing unusual so far. </code>

Setting up the app

According to the tutorial, I have to run django-admin.py startproject mysite command to setup new application. Cool, let’s run it:

No errors, though it generated something. Let's see the tree:

It looks like the initial skeleton. 5 files total. Seems nice and small. The new Rails app has many more directories.

The settings.py file looks like application.rb, the project configuration, although there is no division on various environments.

urls.py reminds me of routes.rb and probably that’s what it’s designed for, we’ll see about that later.

We don’t mind wsgi.py at the moment since we’re not setting up a webserver just yet. The manage.py file looks like equivalent to script/rails, we do various stuff with this command, like starting the server, syncing db (although in rails we do that via rake task).

Starting the app

Using manage.py, we will start up our server:

Yay, it’s working!

The equivalent for Rails will be a rails server command, very easy server startup.

Configuring the database

I’ve already created an empty database, named django_blog and created user django with password django, who has full access to this database.

Let’s configure the database. In the settings.py file, I’ll modify the DATABASES hash to:

In the same file, I’ll also change TIME_ZONE to Europe/Warsaw.

The next step is the INSTALLED_APPS Array, which contains installed apps inside our app.

So it looks managing packages goes here. In Rails there’s a Gemfile file in which we store every gem we want to use for a specific version. At first glance, I can’t specify a version here, but there may be another way.

For now though, I’ll leave it as it is, since it’s the default Array for apps. Let’s first sync the db, as the tutorial says that some of these are using database tables. To sync the db we’re going to use the manage.py script:

Interesting. It asked me for a Django superuser, and validated my random email, which appears to be invalid. Nice. I wonder why it needs this data though. In Rails we have migration files, which every contain changes that can be made to the database, like creating, changing, removing tables and sometimes changes to existing data. But I’ve never come across a migration that would ask for a user input.

Looking at the database, it actually created a record inside auth_user table with my credentials and marked me as a superuser.

Ok I guess we’re ready to use Django, but we will write controllers, models and views in second part or this blog post.

Thanks for reading,

Photo of Jacek Jakubik

More posts by this author

Jacek Jakubik

Jacek hates mushrooms. He loves chocolate. And computer games. And Ruby. Outside of office hours,...
Lost with AI?  Get the most important news weekly, straight to your inbox, curated by our CEO  Subscribe to AI'm Informed

We're Netguru

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency.

Let's talk business