Running Django on Google Colab.

Amit Sindoliya
3 min readDec 22, 2020

--

Hi Everyone, In the midst of this pandemic my primary laptop broke down and my secondary laptop was pretty slow. So, I tried finding alternatives for Django development on the web.

Today, I would like to share one of the way to run Django on Google Colab.

Photo by Kevin Bhagat on Unsplash

Requirements:

  • Mobile, Laptop or any other device which has a web browser and can take input. (Why don’t you try developing a website on a fridge?)
  • A Google Account!
  • Internet Connection (I think you have this step down since you can read this post)

Okay, Here Are The Steps:

Step 1:

Go to Google Colab, Sign In and create a notebook. You can go to google Colab using the below link. Or just search on Google.

https://colab.google.research.com

Step 2:

Now try running this. It should say requirement already satisfied if not it would install Django in your Colab environment. You Need this! You can’t develop on Django without Django.

!pip install django

Since I want to show you guys a working example. I’ll create a new project. You can use the below command if don’t know how to or if you are new to Django. Using the ‘!’ at the start you can run any linux terminal command.

!django-admin startproject portfolio

Use ls to check your directory structure. Below is the output of my project.

Now keep in mind changing your directory i.e. portfolio doesn’t work with an exclamation(!) in Google Colab. You need to use % before your command.

%cd portfolio/

Now basic setup of your Django project is done. Let’s move on to the real thing.

Step 3:

Running this code will give you a link. We Need This Link.

from google.colab.output import eval_jsprint(eval_js("google.colab.kernel.proxyPort(8000)"))

Please note, I have provided a port in the above code. We need to use the same port no. whenever running the Django server. And we need the link for accessing the server from our local machine.

Also you cannot connect to the development server directly. we need to add it in ALLOWED_HOSTS, which is a list in settings.py.

ALLOWED_HOSTS = ['colab.research.google.com']

Now run your server using the below code. And access the server using Google Colab link you printed by running the eval_js method earlier.

DO NOT RUN THE SERVER USING THE SERVER ADDRESS YOU GET AFTER RUNNING THE BELOW COMMAND.

!python manage.py runserver 8000

Hooray!!! We can now work on Django Anywhere, Anytime.

Few Tips for Django Development on Google Colab:

  1. Click the Ham Burger menu on Top-left corner > Now click on the folder icon. This Gives Folder structure. We can double-click and access any file/program.
File Structue

2. We have an half IDE like feel but the files you open are very hard to navigate/edit. Don’t worry this second tip is here for your rescue. Click on the Top- Right page layout icon and change it to Single tabbed view.

Page layout options
Page Layout Options

There you go now you have a Cheap IDE like view for your Django project development.

There are other ways to start a server on Google Colab e.g. ngrok. But I think this is the easiest method.

Photo by Edwin Andrade on Unsplash

--

--