Django on Zeit Now in 30 minutes

The plan

  1. Set up a barebones Django application
  2. Install the Now command-line tools
  3. Configure your project for Now
  4. 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 .

Add an app to the project:

$ python manage.py startapp example_app

Create a view and route

Within the example_app app, create a single view and route. First, a simple view in views.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

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.

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:

  1. "version": 2 tells Now that you’re using a version 2 configuration.
  2. "name": "now-django-example" gives a name to our project. Feel free to change this to whatever your like.
  3. Under "builds", "src": "now_app/wsgi." points to the file that contains a WSGI application declaration (Django created it for you).
  4. "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.
  5. "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.
  6. "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.

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.

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
$ 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]

Further reading

For more on Zeit Now, check out the documentation, the GitHub organization, and the Spectrum group.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store