Git Product home page Git Product logo

vercel-django-example's Introduction

Django running on Vercel

Tutorial

Install Django

$ mkdir vercel-django-example
$ cd vercel-django-example
$ pip install Django
$ django-admin startproject vercel_app .

Add an app

$ python manage.py startapp example

Add the new app to your application settings (vercel_app/settings.py):

# vercel_app/settings.py
INSTALLED_APPS = [
    # ...
    'example',
]

Be sure to also include your new app URLs in your project URLs file (vercel_app/urls.py):

# vercel_app/urls.py
from django.urls import path, include

urlpatterns = [
    ...
    path('', include('example.urls')),
]

Create the first view

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 Vercel!</h1>
            <p>The current time is { now }.</p>
        </body>
    </html>
    '''
    return HttpResponse(html)

Add the first URL

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),
]

Test your progress

Start a test server and navigate to localhost:8000, you should see the index view you just created:

$ python manage.py runserver

Get ready for Now

Add the Now configuration file

Create a new file vercel.json and add the code below to it:

{
    "builds": [{
        "src": "vercel_app/wsgi.py",
        "use": "@ardnt/vercel-python-wsgi",
        "config": { "maxLambdaSize": "15mb" }
    }],
    "routes": [
        {
            "src": "/(.*)",
            "dest": "vercel_app/wsgi.py"
        }
    ]
}

This configuration sets up a few things:

  1. "src": "vercel_app/wsgi.py" tells Vercel that wsgi.py contains a WSGI application
  2. "use": "@ardnt/vercel-python-wsgi" tells Now to use the vercel-python-wsgi builder (you can read more about the builder at https://github.com/ardnt/vercel-python-wsgi)
  3. "config": { "maxLambdaSize": "15mb" } ups the limit on the size of the code blob passed to lambda (Django is pretty beefy)
  4. "routes": [ ... ] tells Now to redirect all requests ("src": "/(.*)") to our WSGI application ("dest": "vercel_app/wsgi.py")

Add Django to requirements.txt

The vercel-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

Update your Django settings

First, update allowed hosts in settings.py to include .now.sh:

# settings.py
ALLOWED_HOSTS = ['.vercel.app']

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 = {}

Deploy

With now installed you can deploy your new application:

$ vercel
Vercel CLI 21.3.3
? Set up and deploy “vercel-django-example”? [Y/n] y
...
? In which directory is your code located? ./
...
✅  Production: https://vercel-django-example.vercel.app [copied to clipboard] [29s]

Check your results in the Vercel dashboard.

vercel-django-example's People

Contributors

jayhale avatar pejowei avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.