Code Monkey home page Code Monkey logo

django-hello-world's Introduction

Deploy with Vercel

Django + Vercel

This example shows how to use Django 4 on Vercel with Serverless Functions using the Python Runtime.

Demo

https://django-template.vercel.app/

How it Works

Our Django application, example is configured as an installed application in vercel_app/settings.py:

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

We allow "*.vercel.app" subdomains in ALLOWED_HOSTS, in addition to 127.0.0.1:

# vercel_app/settings.py
ALLOWED_HOSTS = ['127.0.0.1', '.vercel.app']

The wsgi module must use a public variable named app to expose the WSGI application:

# vercel_app/wsgi.py
app = get_wsgi_application()

The corresponding WSGI_APPLICATION setting is configured to use the app variable from the vercel_app.wsgi module:

# vercel_app/settings.py
WSGI_APPLICATION = 'vercel_app.wsgi.app'

There is a single view which renders the current time in 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)

This view is exposed a URL through example/urls.py:

# example/urls.py
from django.urls import path

from example.views import index


urlpatterns = [
    path('', index),
]

Finally, it's made accessible to the Django server inside vercel_app/urls.py:

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

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

This example uses the Web Server Gateway Interface (WSGI) with Django to enable handling requests on Vercel with Serverless Functions.

Running Locally

python manage.py runserver

Your Django application is now available at http://localhost:8000.

One-Click Deploy

Deploy the example using Vercel:

Deploy with Vercel

django-hello-world's People

Contributors

gepp4 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.