$ mkdir now-django-example
$ cd now-django-example
$ pip install Django
$ django-admin startproject now_app .
$ python manage.py startapp example
Add the new app to your application settings (now_app/settings.py
):
# settings.py
INSTALLED_APPS = [
# ...
'example',
]
Be sure to also include your new app URLs in your project URLs file (now_app/urls.py
):
# now_app/urls.py
from django.urls import path, include
urlpatterns = [
...
path('', include('example.urls')),
]
Add the code below (a simple view that returns the current time) to example/views.py
:
# example/views.py
from datetime import datetime
from django.http import HttpResponse
def index(request):
now = datetime.now()
html = f'''
<html>
<body>
<h1>Hello from Zeit Now!</h1>
<p>The current time is { now }.</p>
</body>
</html>
'''
return HttpResponse(html)
Add the code below to a new file example/urls.py
:
# example/urls.py
from django.urls import path
from example.views import index
urlpatterns = [
path('', index),
]
Start a test server and navigate to localhost:8000
, you should see the index view you just
created:
$ python manage.py runserver
Create a new file now.json
and add the code below to it:
{
"version": 2,
"name": "now-django-example",
"builds": [{
"src": "now_app/wsgi.py",
"use": "@ardnt/now-python-wsgi",
"config": { "maxLambdaSize": "15mb" }
}],
"routes": [
{
"src": "/(.*)",
"dest": "now_app/wsgi.py"
}
]
}
This configuration sets up a few things:
"src": "now_app/wsgi.py"
tells Now thatwsgi.py
contains a WSGI application"use": "@ardnt/now-python-wsgi"
tells Now to use thenow-python-wsgi
builder (you can read more about the builder at https://github.com/ardnt/now-python-wsgi)"config": { "maxLambdaSize": "15mb" }
ups the limit on the size of the code blob passed to lambda (Django is pretty beefy)"routes": [ ... ]
tells Now to redirect all requests ("src": "/(.*)"
) to our WSGI application ("dest": "now_app/wsgi.py"
)
The now-python-wsgi
builder will look for a requirements.txt
file and will
install any dependencies found there, so we need to add one to the project:
# requirements.txt
Django==2.2.4
First, update allowed hosts in settings.py
to include .now.sh
:
# settings.py
ALLOWED_HOSTS = ['.now.sh']
Second, get rid of your database configuration since many of the libraries django may attempt to load are not available on lambda (and will create an error when python can't find the missing module):
# settings.py
DATABASES = {}
With now installed you can deploy your new application:
$ now
> Deploying now-django-example under jayhale
...
> Success! Deployment ready [57s]
Check your results by visiting https://zeit.co/dashboard/project/now-django-example