Raspberry Pi Django Tutorial

1. Setup Project Files Raspberry Pi Django Web App

This is a Raspberry Pi Tutorial Django project that can be found at: https://github.com/mavieth/Raspberry-Pi-Django-Web-App

mkdir -p ~/projects/django-rpi
cd ~/projects/django-rpi/

2. Install Virtual Environment for Django Web App

This is so we can install python packages without having to run with the sudo command for our project.

pip install virtualenv
sudo pip install virtualenv

3. Create Virtual Environment and Activate

After this command you should have an (env) before your terminal to let you know that you have activated the virtual environment.

virtualenv venv && source venv/bin/activate

4. Install Django on your Raspberry Pi, and Create the Project Files

This may take a few minutes as it will be install Django to run on your Raspberry Pi

pip install django
django-admin startproject raspberrypiapp
cd raspberrypiapp


On line in ~/projects/django-rpi/raspberrypiapp/raspberrypiapp/settings.py , where is the IP address of your Raspberry Pi

ALLOWED_HOSTS = ["localhost",""]

7. Migrate Changes and Start the Server!

python manage.py migrate

python manage.py runserver


This will start an application on http://localhost:8080 or from another computer on the same network where is the IP address of your Raspberry Pi.



You can find my project files at https://github.com/mavieth/Raspberry-Pi-Django-Web-App

Please comment below if you have any questions!

You may also like...

11 Responses

  1. Laslarailas says:

    Hours trying make pip works on my RasPy, finally this command do the installation of pip: sudo apt-get install python-pip

  2. Andrew says:

    Are you going to update part 2 of the original tutorial?

    • RPi Admin says:

      Those tutorials were functional at the time posted, so I will leave them as is. This should be used for people who have more recently updated OS’s on their pi.

  3. Mark says:

    Super straight forward and easy – got Django up and running on Arch on my Raspberry Pi in minutes. Thanks!

  4. Martin Harvey says:

    ALLOWED_HOSTS = [“localhost”,”″]

    What happen to number 5 ?
    You have 1 to 4 then 6 & 7
    Many thanks

    • RPi Admin says:

      Hmmm…. I am not sure why I skipped #5. I do not believe that I left a step out, it may just have been an error in my copywriting.

      -A man of many mistakes

  5. Scott says:

    All i get is Traceback (most recent call last):
    File “manage.py”, line 17, in
    “Couldn’t import Django. Are you sure it’s installed and ”
    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?
    when i try to run step 7.

    • P.Nut says:

      Scott – don’t know if you’re still stuck, but something rings a bell saying you need to reactivate the virtual environment every time you try to go back to what you were doing. Command looks something like ‘workon myproject’

    • RPi Admin says:

      If your virtual env folder is named venv. You can run the command source venv/bin/activate. That will activate the python packages installed for the environment.

  6. Daniel Hennessy says:

    Could not get past step 7 – “python manage.py migrate”
    until I changed manage.py to look like:

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

    if __name__ == “__main__”:
    os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “raspberrypiapp.settings”)
    from django.core.management 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
    except ImportError: # from exc:

    I am not sure if line 15 – “except ImportError:” is correct, but with it that way, everything seems to work perfectly.

  7. Fabian says:

    Couldn’t get Django to install following your steps at first, but it was because on my pi 3 pip refers to python2 pip, and I therefore had to use pip3 to refer to python3. The latest Django requiring python3. The error I got during install didn’t make that clear but after some googling I found the issue was with python3 not being used. Fyi, you might want to make that clear in the first few steps. Thanks for the great tutorial.

Leave a Reply to Mark 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.