Django static files on Zeit Now
Zeit Now is a slick wrapper around AWS Lambda that makes getting simple web-apps up and running a matter of running a single command —
This guide will help you deploy your static files alongside a Django app you have running on Now. If you’re just getting started with Django on Now, check out this other article that walks you through deploying Django on Now in 30 minutes or less.
tl;dr: This guide shows you how to deploy your Django static directory using Now’s static serving tools — letting Now handle compression and versioning for you — by running a custom build script with the
- Set up a basic Django project
- Adjust your static files settings in Django
- Add a custom script for building static files on Now
- Update your Now config and deploy
1. Set up a basic Django project
There’s nothing special here, so if you have an existing project skip to #2.
Start a new Django project:
$ mkdir now-django-staticfiles && cd now-django-staticfiles
$ pip install Django
$ django-admin startproject now_app .
You now should have a Django app in
now-django-staticfiles/. Check to make sure everything happened as expected by running the development server:
$ python manage.py runserver
$ open http://localhost:8000
For deploying, we’ll need to add our Django dependency to a requirements file. Create a new
requirements.txt file in your project root with:
2. Adjust your static files settings
We’ll need to configure Django’s static settings so that we can collect all of our static files to a single directory and allow Now to deploy just that folder.
now_app/settings.py or your own app’s settings file and ensure
STATIC_ROOT are set as shown:
STATIC_ROOT is set to a subdirectory to make routing with Now easier. If
STATIC_ROOT were set to something like
staticfiles/, we would have to set up a route pattern for each file. Using
staticfiles_build/static/ allows us to tell Now to deploy the whole directory
static/ and set up a single route to that folder (which we’ll do in step 4).
3. Add a staticfiles build script
Now’s @now/static-build builder allows us to run a custom bash script and deploy any files populated in a subdirectory of the build. This allows us to run
python manage.py collectstatic in every build instead of having to run it before each deploy.
Create a new file
now_build_staticfiles.sh in your project root with the contents below:
The script takes a few preparation steps and then collects static files for your app:
yum install -y https://centos6...installs the IUS (“inline with upstream stable”) repository to make a Python 3.6 package available.
yum install -y python36uinstalls Python 3.6
python 3.6 get-pip.pyinstall Pip for the new Python 3.6 installation.
pip install -r requirements.txtinstalls any project requirements.
python3.6 manage.py collectstaticfinally collects staticfiles into the directory we configured in
4. Update/create a Now config and deploy
You’ll need to make two additions to your Now configuration to handle building and routing for static files. Update or create a
now.json file in the project root to match the contents below:
Here’s what this configuration sets up for you:
buildssection creates two separate builds: the first will run
now_build_staticfiles.shand then deploy the resulting files using the
@now/static-buildbuilder; the second will build a lambda against
routessection handles two routes patterns, in order: first it will match any requests to
/static/...and pass those on to the static files directory, to be served by Now’s file serving systeml; second, it matches all other requests and passes them on your your application.
You can now deploy both your application lambda and static files in a single step:
Voilà! Well done!
For help getting started with Django on Now, check out my other article: Django on Zeit Now in 30 minutes.
Enjoy your time with Now and best of luck with your own projects! Please feel free to comment with any questions, challenges, or corrections.