Django + Raspberry Pi Tutorial (PART I)

UPDATE (1/11/2017): The new tutorial can be found at:

This tutorial will walk you through how to setup your Raspberry Pi with Django. If you have any problems setting up the Django + Raspberry Pi Tutorial (PART I) setup be sure to comment questions or suggestions, be sure to post them at the bottom of the page. If you’d like to become a contributor or edit for the Django + Raspberry Pi Tutorial (PART I) please feel free to contact me.

Start: Django + Raspberry Pi Tutorial (PART I)

Step 1. Updating and Cleaning your Raspberry Pi

sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get autoremove

Step 2. Install Python MySQLDB

sudo apt-get install python-mysqldb -y

Step 3. Install LAMP Server

The second install on this step will ask you to create a password for the root SQL user.

sudo apt-get install apache2 -y
sudo apt-get install mysql-server mysql-client -y
sudo apt-get install php5 libapache2-mod-php5 php5-mysql -y
sudo service apache2 restart

Step 4. Install PHPMyAdmin and configure it to Apache.

This step will ask you which server you would like to use for your application. I chose Apache.

sudo apt-get install phpmyadmin -y
sudo nano /etc/apache2/apache2.conf
In this you’ll see include statements, find an empty line right after the include conf.d/ that has an empty line after it. Go down until are on the empty line. Type the following:
Include /etc/phpmyadmin/apache.conf
Restart the server to be able to access phpmyadmin on your local server (in a browser go to localhost/phpmyadmin).
sudo service apache2 restart

Step 5. Install Python Setup tools

sudo apt-get install python-setuptools -y

Need a new SD Card?

Step 6. Make sure you have PIP installed

sudo python
sudo rm -rf

Step 7: Install Django

sudo pip install Django

Step 8: Creating our application

I will use the name DjangoProject but you can call yours whatever you prefer.

django-admin startproject DjangoProject
cd DjangoProject

Step 9: Migrating DB and Running App

Migrate the DB and starting our application. Use your Raspberry Pi IP address (mine is and choose what port you’d like to run on.

python migrate
python runserver

You can then head to from your computer and you should see this if everything worked properly.


Step 10: Customize our App

Check out the next tutorial.

You may also like...

12 Responses

  1. chris says:

    this is nice tut however i still dont understand how this makes use of the apache server we installed? its still using the django devopment server.?

  2. chris: I agree. The Apache2 server actually causes problems with the standard HTTP port 80 for the Django app, since Apache2 occupy that port. If you want your Django app to use the normal HTTP port 80 you have to stop apache2 and make sure it does not start at boot.

    Stop apache2 service: sudo service apache2 stop
    Stop apache2 from starting at boot, in /etc/init.d/: sudo chmod -x apache2

    After this you can start your Django server with: python runserver
    Make a shell script ( in /etc/init.d/ holding the above command and “sudo chmod +x” to start your Django site at boot.

  3. Just note that as of 11/19/2016 the updated location for is

    Hence step 6 should be changed to: wget

    Hope that helped 🙂

  4. Cancelor says:

    I found that on step 9 I had to use python3 instead of just python to run those two lines.

    Also, when I smurfed to or even localhost:8000 I got a different screen that states :- “You may need to add ‘’ to ALLOWED_HOSTS”

    • aaron says:

      I think I am having a similar issue. When I try to navigate to the sample page through my browser I get an error message saying ‘DisallowedHost at /’, then there is a bunch of stuff I do not understand.

      When I tried your suggestion of using python3 I also get an error saying that there is no Django module installed.

      Any ideas? (I am a little over my head with some of this stuff, just a beginner to linux and python/django).

  5. Amirul says:

    Go to add your ip or or your domain under ALLOWED_HOSTS = []
    Like this :
    ALLOWED_HOSTS = [ ‘ ‘ ] # for internal ip
    ALLOWED_HOSTS = [ ‘’ ]# for domain name install

  6. Daniel Hennessy says:

    I am having trouble at step 7.
    This is my “” file

    #!/usr/bin/env python
    import os
    import sys

    if __name__ == “__main__”:
    os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “raspberrypiapp.settings”)
    from import execute_from_command_line
    except ImportError as exc:
    raise ImportError(
    “Couldn’t import Django. Are you sure it’s installed and ”
    “available on your PYTHONPATH environment variable? Did you ”
    “forget to activate a virtual environment?”)
    from exc

    When I run python migrate, I get the following error
    (venv) pi@raspberrypi:~/projects/django-rpi/raspberrypiapp $ python migrate
    File “”, line 14
    from exc
    SyntaxError: invalid syntax

    What should proper syntax be in
    Thank you for any help you can provide.

  7. bahiano says:

    hi. i’m having trouble installing libapache2-mod-php5, php5, and php5-mysql… seems like now it’s all about php7. Anyone knows which commands are actually accurate to acomplish installing the LAMP with php7

  1. May 19, 2015

    […] This tutorial will walk you through how to setup your first Django Web Application. It is based off of the work done in the previous tutorial. […]

Leave a Reply to RPi Admin Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.