Setting Up Django on the Raspberry Pi

A step-by-step guide on installing Django on the Raspberry Pi and accessing it from Windows.

Django is a web framework for perfectionists (with deadlines). It lets you build high-performing, elegant web applications quickly using the Python programming language. A web framework is a software framework designed to simplify the web development process.  Basically, web sites have a common set of functionality such as accessing a database, managing sessions and cookies and creating templates to display your HTML. Web frameworks help alleviate the overhead of having to code all these functionalities. In other words a web framework saves you from having to re-invent the wheel and makes it easier for you to build your website or web application.

Getting a fresh copy of Raspbian

Raspbian is the operating system of choice and is used throughout this guide. If 20170627_143341you have an existing Raspbian running on your Raspberry Pi you can skip this step and proceed to the next one. Otherwise, you will need to download a copy of Raspbian Jessie Lite from the Raspberry Pi website and burn the Raspbian image on your SD card. You can read the instructions if you need help with the installation. After burning the image, boot your Raspberry Pi and log in with the user name pi and with the password raspberry.


Enabling SSH

SSH (Secure Shell) is disabled by default on Raspbian. We need to enable it so that we could connect to our Pi from Windows. In the command line type:

$ sudo raspi-config

This will bring up the Raspberry Pi Configuration Tool. From the choices select Interfacing Options.


Then select SSH.


You will then be asked if you would like the SSH server to be enabled, choose yes then enter.


A message will appear saying The SSH server is enabled. Just press enter to go back to the main menu.


Before exiting raspi-config you also might want to change other configurations like your locale and timezone. Select finish to exit raspi-config.


Now connect your Raspberry Pi to your router with an ethernet cable and reboot your Pi with:

$ sudo reboot

Connecting to your Pi using PuTTY

We’re going to use a program called PuTTY to connect to our Raspberry Pi. You can download PuTTY here. What we need is the putty.exe under the Alternative binary files. If you have a 64-bit installation of Windows then you can get the 64-bit version, otherwise the 32-bit version would do.

Before we can connect to our Raspberry Pi we need to find out its IP address, so go back and log in again to your Raspberry Pi and in the command line type:

$ hostname -I

Remember the output of the command, this is your Raspberry Pi’s IP address and we will use this to connect to your Pi. Now run the putty.exe that you downloaded, and in the Host Name (or IP address) box enter your Raspberry Pi’s IP address then click on Open.


If everything goes well then you should be able to log in remotely with the user name pi and the raspberry password. If not then go back and check your internet connection.


Creating a Python Virtual Environment and installing Django

We need to create an isolated Python environment so that our system installation of Python will be untouched when we install Django. We need a package called virtualenv to do that. To install virtualenv type the following commands:

$ sudo apt-get update
$ sudo apt-get install -y virtualenv

To actually create the virtual environment type:

$ virtualenv --python=/usr/bin/python3 ~/.venv

We want to make Python 3 our virtual environment’s Python interpreter and that is what the --python=/usr/bin/python3 option is for.  To activate the virtual environment we just created type:

$ source ~/.venv/bin/activate

To deactivate the virtual environment just type deactivate.

Installing Django is simple as Pie. We can just get the package from PyPI with:

$ pip install django

Creating our first project

Before we create our first project let’s create a new directory and change into that directory.

$ mkdir django
$ cd django

To create a project type (in some cases you may have to deactivate and re-activate your Python virtual environment before you can invoke

$ startproject mysite

This will create a directory called mysite, which contains the following files and directory.

└── mysite

Running the development server

Django comes equipped with a development server. Before we run the server we need to edit the file to include your Raspberry Pi’s IP address to allowed hosts. Make sure you’re on your project’s root directory and edit your with nano.

$ cd mysite
$ pwd
$ nano mysite/

Find the line that says ALLOWED_HOSTS = [] and place your Raspberry Pi’s IP address inside the square brackets enclosed in quotation marks followed by a comma like this.


Save the file and exit nano. This allows us to browse our development server from our web browser.
Note: IP addresses change from time to time. To save you from trouble of having to edit your when your Raspberry Pi’s IP address change, it’s a good idea to give your Raspberry Pi a static IP address. This link gives a very good tutorial on setting up a static IP address on your Raspberry Pi.

We’re almost ready, but before we start running the server there is one last command. On your project’s root directory type:

$ python migrate

To run the server type:

$ python runserver

You can now access your project by entering your Raspberry Pi’s IP address, colon then port number 8000 (in my case it’s into your web browser’s  address bar. You should see something like this, which is Django’s default page.


Congratulations! You have successfully set up Django on your Raspberry Pi and created your first Django project. On our next tutorial we will change the default page and learn about views and urls.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s