Django Hello World

R
Renaudil y a 2 jours
0

Description

Use Django 4 on Vercel with Serverless Functions using the Python Runtime.

npx boilerapp django-hello-world

文档

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 api/settings.py:

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

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

# api/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:

# api/wsgi.py
app = get_wsgi_application()

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

# api/settings.py
WSGI_APPLICATION = 'api.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 api/urls.py:

# api/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

Prix

Gratuit

FREE

评论 (0)

常见问题

常见问题解答 (FAQ)

有问题?我们有答案。如果您找不到想要的答案,请随时联络我们。

Boilerapp 是一个专门用于分享 Boilerplates、入门套件(Starter Kits)和项目模版的开发者社区平台。我们的目标很简单:为您节省初始配置(Setup)的时间,让您可以专注于真正重要的代码。无论您是在寻找简单的代码库还是完整的 SaaS 项目,都能在这里找到。

还有其他问题?

我们的团队随时为您提供帮助。联络我们,我们将尽快回复。

Django Hello World | BoilerApp