Django + Raspberry Pi Tutorial (PART II)
UPDATE (1/11/2017): The new tutorial can be found at: http://raspberrypituts.com/raspberry-pi-django-tutorial-2017/
This tutorial will walk you through how to setup your first Django Web Application on your Raspberry Pi. The Django + Raspberry Pi Tutorial is is based off of the work done in the previous tutorial – Django + Raspberry Pi Tutorial (PART I).
Step1: Change settings.py
On line 35 change:
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'FirstApp', )
Note: I added ‘FirstApp’ which is the name of the application we created in the last tutorial.
Step 2: Change urls.py
Django uses a mapping file called urls.py which maps html addresses to views, using regular expressions. Django maps a requested url to a view which is needed for a response via regular expressions. My urls.py file looks like this:
from django.conf.urls import include, url from django.contrib import admin from FirstApp.views import foo urlpatterns = [ # url(r'^admin/', include(admin.site.urls)), url(r'FirstApp/$', foo), ]
Step 3: Head over to our /DjangoProject/FirstApp/views.py for an edit
cd cd DjangoProject/FirstApp/
My DjangoProject/FirstApp/views.py file looks like this
from django.shortcuts import render from django.http import HttpResponse def foo(request): return HttpResponse("Hello World!")
The foo is the Python import string to get to a view.
Step 4: Start the server
cd cd FirstDjangoApp python manage.py runserver 192.168.0.5:8000
Step 5: Test it out!
Head over to http://192.168.0.5:8000 and you should see “Hello World!” Comment if you have any issues or suggestions.