Creating a Django app
A simple Django project to get started
We'll need a simple Django app to demonstrate the Zappa functionality.
Establish a virtual environment and install packages#
We will run most of the commands in our Docker instance so that all packages installed will be appropriate for the AWS Lambda environment. If you run these commands on your local system, you may install versions of the package that will fail when executed in the cloud. Start your Docker instance by running on Mac or Linux:
Or if you're on Windows:
You should see the
Now create a virtual environment, activate it, and install Django & Zappa. Note we are calling the virtual environment
ve. Note the command prompt will change to indicate that the virtual environment is active.
One of the nice things about running in Docker is that any Python build / compiling is done using a near-Lambda environment. This allows us to be confident that our app will work fine when in production.
Start a Django Project#
We'll call our simple project 'Frankie.' To have the Django Admin generate code for the Django project, simply run:
And now let's run it on our local system to see if it's working. Note that we are running the Django project within the Docker instance.
This is the common Django runserver command to launch a small web server so we can debug our Django application. There's a slight difference from the typical usage you may have seen: we have added
0:8000 to the runserver command. This is because we're running in a virtual environment and this enables the debug server to be seen by the host system. If you omit this portion of the command line, you won't be able to use the browser on your local machine to view the page.
Now, point your browser at http://localhost:8000. You should see the default Django page.
Uh-oh! Actually, we don't see the default Django page - we see a bunch of errors that should end with something like this:
Bummer. This is caused by the fact that most Python installations bundle SQLite, a helpful library that lets you run a little database locally. However, the AWS Lambda version of Python does not include this library. Fortunately, we won't need SQLite for this course, so we have to comment out some lines in our Django settings. Edit the file
frankie\settings.py and comment out the
Don't worry - we'll add back database support in the Module about Databases. For now, we're just getting this up and running locally.
Now point your browser at http://localhost:8000. You should see the default Django page.
Now let's create a simple view to power this Django app so it's a little more interesting than the default view. Run the following command to add a Django application to the project:
guitar/views.py to create a view that sends a brief greeting:
guitar/urls.py to wire up the new view we created:
frankie/urls.py to add the
guitar Django application:
Now refresh your browser and you should see our message in the browser window
Cool, huh? In the next lesson, we will get this loaded in the cloud!