Tested on:RPi3; Raspbian Stretch; Geckodriver 0.21.0; Python 3.6.3; Django 1.11.14; Selenium 3.13.0
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:(2 LEDs with resistors and a breadboard or soldering equipment)
3

Python-Webframework Django on the Raspberry Pi


Django is a wonderful web framework in Python and allows creating up-to-date web applications with comparably low effort. For simple Raspberry Pi remote controls, CherryPy is sufficient: for more complex tasks like setting up an user management, Django has to be recommended strongly.
This tutorial shows how to install Django first. Then, a demo project for creating lists with a web interface from the outstanding and freely available ebook Test-Driven Development with Python by Harry Percival (Twitter) is extended for status signals with an LED.

Installation
The whole installation takes a while. First, enter in the terminal

Then, follow the steps described in the tutorial Python3.6 Installation.
Finally, enter these commands:

Django Demo
In the video, i’ve attached a blue LED to pin 40 and a green one to pin 38 in BOARD-numbering. When a new list is created, the green LED blinks; if a new list entry is added, the blue LED blinks. But the application runs without LEDs attached as well.
Run the demo application by entering

Now, open a browser and browse to 127.0.0.1:8000:

Django lists

Django lists

Django is a RESTful API which resolves URLs entered in a browser to Python functions. For example, the file views.py has a function add_item:
With the passed list_id, .create creates a new database entry with the text of the new list entry. blink makes the LED blink with RPi.GPIO, see Full script.

Full script
Click on Full Script to show all of the code.

Links
Tutorial Python3.6 Installation: http://www.knight-of-pi.org/de/python3-6-installation-auf-dem-raspberry-pi/
Django Website: https://www.djangoproject.com
Tutorial CherryPy: http://www.knight-of-pi.orgcherrypy-an-elegant-python-webserver-for-raspberry-pi-remote-controls/
Ebook Test-Driven Development with Python: https://www.ebooks.com/95836888/test-driven-development-with-python/percival-harry-j-w/
Twitter Harry Percival: https://twitter.com/hjwp

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.

3 Comments

  1. With advanced Python programming concepts, developers can homebrew their own gadgets, and connect them with real world markets independently and on the cheap.

  2. I’ve been thinking of playing with Django and this was helpful. Thank you Mr. Knight of Pi!

    • Glad you like it!

      If you have some time on your hands, work through the book of Harry Percival which i linked in the tutorial-
      it has everything you need for getting started with Django and much more!

Comment

Time limit is exhausted. Please reload CAPTCHA.