This guide will help you get a barebones Django application working by taking advantage of a custom Now builder for python WSGI applications: @ardnt/now-python-wsgi.
At Ardent, we’re using the builder to experiment with Now for a handful of public APIs that were built for Lambda, so you can expect it to mature along with Zeit until an official WSGI builder becomes available.
tl;dr: To cut to the chase, you can see a previous version of this tutorial along with the finished project on GitHub.
The plan
- Set up a barebones Django application
- Install the Now command-line tools
- Configure your project for Now
- Deploy!
1. Set up a barebones Django application
For the sake of example, we’ll work with what Django gives us out of the box with minimal additions.
Start a new Django project:
$ mkdir now-django-example && cd now-django-example
$ pip install Django
$ django-admin startproject now_app .
This establishes a new Django project in now-django-example/
. I’m skipping over any environment management, assuming you have your own preferences (but check out direnv if you’re in the market).
Your project needs to be compatible with the python 3.6 runtime. Take care when you’re establishing your environment to select the right python version.
Add an app to the project:
$ python manage.py startapp example_app
This creates a stubbed application in now-django-example/example_app
.
Update your project settings to include the new app by adding 'example_app'
to the end of the INSTALLED_APPS
list:
And update your project URLs to include URLs from the new app:
Create a view and route
Within the example_app
app, create a single view and route. First, a simple view in views.py
:
And our route in urls.py
:
Check our progress
You now have a functioning Django application. You can test it out by starting up the development server and navigating to http://localhost:3000/:
$ python manage.py runserver
Step 1 complete! 🎉
2. Install the Now command-line tools
Our next order of business is to get linked up to Zeit Now. The folks at Zeit have created a set of command line tools, similar to Serverless, that you need for managing your projects.
Create a Zeit account
If you haven’t already yet, create a Zeit account at https://zeit.co/signup. You’ll be taken through a brief onboarding, all of which you can skip except for the initial step of choosing a username.
Install the Now toolchain
The desktop app offers a slick experience and includes the command line tools you’ll need to deploy. Download and install the appropriate version from https://zeit.co/download.
You’re now nearly ready to deploy! 👏
3. Configure your project for Now
There are three chores to take care of before you’re ready for deployment: adding a Now configuration, fixing some settings, and listing our dependencies.
Add a Now configuration
First, and most importantly, add a Now configuration file, now.json
, to the root of your project:
This configuration does a few things:
"version": 2
tells Now that you’re using a version 2 configuration."name": "now-django-example"
gives a name to our project. Feel free to change this to whatever your like.- Under
"builds"
,"src": "now_app/wsgi."
points to the file that contains a WSGI application declaration (Django created it for you). "use": "@ardnt/now-python-wsgi"
tells Now to use the@ardnt/now-python-wsgi
builder. The latest version of the builder will be installed from NPM when you deploy our project during the build step."config": { "maxLambdaSize": "15mb" }
increases the size limit of our bundle (the default is 5MB) — this may not be necessary, but Django is a large package, so it saves a headache if size becomes an issue. Feel free to experiment with the appropriate upper limit."routes": [ ... ]
rewrites all routes ("src": "/(.*)"
) to our WSGI application ("dest": "now_app/wsgi.py"
) to be handled.
Update your Django settings
Next, you’ll need to adjust two Django project settings.
First, update ALLOWED_HOSTS
to allow now.sh
to be a valid hostname for your app.
Second, clear out your settings for DATABASES
— this will prevent Django from searching for a database adapter (which is not available given the barebones nature of the Lambda compute environment).
Enumerate dependencies
Finally, you’ll need to list out dependencies in a requirements.txt
file so that the Now builder knows what to install for you before deploying.
That’s it! You’re ready to go! 🍰
4. Deploy!
A little bit of configuration pays big dividends for your next step. To get a live working version of your app, all you need to do is run:
$ now
That’s it! After the build completes, your application will be available.
$ now
> Deploying now-django-example under jayhale
> Using project now-django-example
> https://now-django-example-kuv5rkvrz.now.sh [v2] [in clipboard]
┌ index.py Ready [18s]
└── λ index.py (636.23KB) [iad1]
> Success! Deployment ready [21s]
Well done!
Further reading
For more on Zeit Now, check out the documentation, the GitHub organization, and the Spectrum group.
Enjoy your time with Now and best of luck with your own projects! Please feel free to comment with any questions, challenges, or corrections.