From 48ced6e5b497c5d8dcb0ad643a640e639cb8b0a4 Mon Sep 17 00:00:00 2001 From: dnwpark Date: Fri, 13 Mar 2026 10:56:59 -0700 Subject: [PATCH 1/3] Update python/django example. --- python/django/README.md | 51 ++++------- python/django/api/settings.py | 121 ------------------------- python/django/api/wsgi.py | 16 ---- python/django/{api => app}/__init__.py | 0 python/django/{api => app}/asgi.py | 6 +- python/django/app/settings.py | 118 ++++++++++++++++++++++++ python/django/{api => app}/urls.py | 11 ++- python/django/app/wsgi.py | 16 ++++ python/django/example/apps.py | 6 -- python/django/example/urls.py | 4 +- python/django/example/views.py | 7 +- python/django/manage.py | 5 +- python/django/pyproject.toml | 7 ++ python/django/requirements.txt | 1 - python/django/vercel.json | 8 -- 15 files changed, 179 insertions(+), 198 deletions(-) delete mode 100644 python/django/api/settings.py delete mode 100644 python/django/api/wsgi.py rename python/django/{api => app}/__init__.py (100%) rename python/django/{api => app}/asgi.py (59%) create mode 100644 python/django/app/settings.py rename python/django/{api => app}/urls.py (78%) create mode 100644 python/django/app/wsgi.py delete mode 100644 python/django/example/apps.py create mode 100644 python/django/pyproject.toml delete mode 100644 python/django/requirements.txt delete mode 100644 python/django/vercel.json diff --git a/python/django/README.md b/python/django/README.md index 3e81ff2d42..ca4d387c0c 100644 --- a/python/django/README.md +++ b/python/django/README.md @@ -1,8 +1,8 @@ -[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Fdjango&demo-title=Django%20%2B%20Vercel&demo-description=Use%20Django%204%20on%20Vercel%20with%20Serverless%20Functions%20using%20the%20Python%20Runtime.&demo-url=https%3A%2F%2Fdjango-template.vercel.app%2F&demo-image=https://assets.vercel.com/image/upload/v1669994241/random/django.png) +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Fdjango&demo-title=Django%20%2B%20Vercel&demo-description=Use%20Django%206%20on%20Vercel%20with%20Serverless%20Functions%20using%20the%20Python%20Runtime.&demo-url=https%3A%2F%2Fdjango-template.vercel.app%2F&demo-image=https://assets.vercel.com/image/upload/v1669994241/random/django.png) # Django + Vercel -This example shows how to use Django 4 on Vercel with Serverless Functions using the [Python Runtime](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python). +This example shows how to use Django 6 on Vercel with Serverless Functions using the [Python Runtime](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python). ## Demo @@ -10,35 +10,21 @@ https://django-template.vercel.app/ ## How it Works -Our Django application, `example` is configured as an installed application in `api/settings.py`: +Our Django application, `example` is configured as an installed application in `app/settings.py`: ```python -# api/settings.py +# app/settings.py INSTALLED_APPS = [ # ... - 'example', + "example", ] ``` -We allow "\*.vercel.app" subdomains in `ALLOWED_HOSTS`, in addition to 127.0.0.1: +We allow `*.vercel.app` subdomains in `ALLOWED_HOSTS`, in addition to `127.0.0.1` and `localhost`: ```python -# 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: - -```python -# api/wsgi.py -app = get_wsgi_application() -``` - -The corresponding `WSGI_APPLICATION` setting is configured to use the `app` variable from the `api.wsgi` module: - -```python -# api/settings.py -WSGI_APPLICATION = 'api.wsgi.app' +# app/settings.py +ALLOWED_HOSTS = ["127.0.0.1", "localhost", ".vercel.app"] ``` There is a single view which renders the current time in `example/views.py`: @@ -52,18 +38,18 @@ from django.http import HttpResponse def index(request): now = datetime.now() - html = f''' + html = f"""

Hello from Vercel!

The current time is { now }.

- ''' + """ return HttpResponse(html) ``` -This view is exposed a URL through `example/urls.py`: +This view is exposed through `example/urls.py`: ```python # example/urls.py @@ -73,19 +59,19 @@ from example.views import index urlpatterns = [ - path('', index), + path("", index), ] ``` -Finally, it's made accessible to the Django server inside `api/urls.py`: +Finally, it's made accessible to the Django server inside `app/urls.py`: ```python -# api/urls.py +# app/urls.py from django.urls import path, include + urlpatterns = [ - ... - path('', include('example.urls')), + path("", include("example.urls")), ] ``` @@ -94,7 +80,8 @@ This example uses the Web Server Gateway Interface (WSGI) with Django to enable ## Running Locally ```bash -python manage.py runserver +uv sync +uv run python manage.py runserver ``` Your Django application is now available at `http://localhost:8000`. @@ -103,4 +90,4 @@ Your Django application is now available at `http://localhost:8000`. Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples): -[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Fdjango&demo-title=Django%20%2B%20Vercel&demo-description=Use%20Django%204%20on%20Vercel%20with%20Serverless%20Functions%20using%20the%20Python%20Runtime.&demo-url=https%3A%2F%2Fdjango-template.vercel.app%2F&demo-image=https://assets.vercel.com/image/upload/v1669994241/random/django.png) +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Fdjango&demo-title=Django%20%2B%20Vercel&demo-description=Use%20Django%206%20on%20Vercel%20with%20Serverless%20Functions%20using%20the%20Python%20Runtime.&demo-url=https%3A%2F%2Fdjango-template.vercel.app%2F&demo-image=https://assets.vercel.com/image/upload/v1669994241/random/django.png) diff --git a/python/django/api/settings.py b/python/django/api/settings.py deleted file mode 100644 index 0c39dd5f61..0000000000 --- a/python/django/api/settings.py +++ /dev/null @@ -1,121 +0,0 @@ -""" -Django settings for api project. - -Generated by 'django-admin startproject' using Django 4.1.3. - -For more information on this file, see -https://docs.djangoproject.com/en/4.1/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/4.1/ref/settings/ -""" - -from pathlib import Path - -# Build paths inside the project like this: BASE_DIR / 'subdir'. -BASE_DIR = Path(__file__).resolve().parent.parent - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-=cldztbc4jg&xl0!x673!*v2_=p$$eu)=7*f#d0#zs$44xx-h^' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = ['127.0.0.1', '.vercel.app'] - - -# Application definition - -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'example' -] - -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'api.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, -] - -WSGI_APPLICATION = 'api.wsgi.app' - - -# Database -# https://docs.djangoproject.com/en/4.1/ref/settings/#databases -# Note: Django modules for using databases are not support in serverless -# environments like Vercel. You can use a database over HTTP, hosted elsewhere. - -DATABASES = {} - - -# Password validation -# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/4.1/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/4.1/howto/static-files/ - -STATIC_URL = 'static/' - -# Default primary key field type -# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field - -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/python/django/api/wsgi.py b/python/django/api/wsgi.py deleted file mode 100644 index f5e3ce54f2..0000000000 --- a/python/django/api/wsgi.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -WSGI config for api project. - -It exposes the WSGI callable as a module-level variable named ``app``. - -For more information on this file, see -https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ -""" - -import os - -from django.core.wsgi import get_wsgi_application - -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings') - -app = get_wsgi_application() diff --git a/python/django/api/__init__.py b/python/django/app/__init__.py similarity index 100% rename from python/django/api/__init__.py rename to python/django/app/__init__.py diff --git a/python/django/api/asgi.py b/python/django/app/asgi.py similarity index 59% rename from python/django/api/asgi.py rename to python/django/app/asgi.py index 8f60ecc6f5..91e1d4f13f 100644 --- a/python/django/api/asgi.py +++ b/python/django/app/asgi.py @@ -1,16 +1,16 @@ """ -ASGI config for api project. +ASGI config for app project. It exposes the ASGI callable as a module-level variable named ``application``. For more information on this file, see -https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ +https://docs.djangoproject.com/en/6.0/howto/deployment/asgi/ """ import os from django.core.asgi import get_asgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings') +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") application = get_asgi_application() diff --git a/python/django/app/settings.py b/python/django/app/settings.py new file mode 100644 index 0000000000..1d6a88208f --- /dev/null +++ b/python/django/app/settings.py @@ -0,0 +1,118 @@ +""" +Django settings for app project. + +Generated by 'django-admin startproject' using Django 6.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/6.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "django-insecure-change-me-in-production" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ["127.0.0.1", "localhost", ".vercel.app"] + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "example", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "app.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "app.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/6.0/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/6.0/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/6.0/howto/static-files/ + +STATIC_URL = "static/" diff --git a/python/django/api/urls.py b/python/django/app/urls.py similarity index 78% rename from python/django/api/urls.py rename to python/django/app/urls.py index 4810f2a444..2e61a80b92 100644 --- a/python/django/api/urls.py +++ b/python/django/app/urls.py @@ -1,7 +1,8 @@ -"""api URL Configuration +""" +URL configuration for app project. The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/4.1/topics/http/urls/ + https://docs.djangoproject.com/en/6.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views @@ -13,10 +14,12 @@ 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ + from django.contrib import admin from django.urls import path, include + urlpatterns = [ - path('admin/', admin.site.urls), - path('', include('example.urls')), + path("admin/", admin.site.urls), + path("", include("example.urls")), ] diff --git a/python/django/app/wsgi.py b/python/django/app/wsgi.py new file mode 100644 index 0000000000..373e747510 --- /dev/null +++ b/python/django/app/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for app project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") + +application = get_wsgi_application() diff --git a/python/django/example/apps.py b/python/django/example/apps.py deleted file mode 100644 index 7418128b8d..0000000000 --- a/python/django/example/apps.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.apps import AppConfig - - -class ExampleConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'example' diff --git a/python/django/example/urls.py b/python/django/example/urls.py index d998d717c3..ddaf8f7949 100644 --- a/python/django/example/urls.py +++ b/python/django/example/urls.py @@ -5,5 +5,5 @@ urlpatterns = [ - path('', index), -] \ No newline at end of file + path("", index), +] diff --git a/python/django/example/views.py b/python/django/example/views.py index c006d7b79c..fba18ce977 100644 --- a/python/django/example/views.py +++ b/python/django/example/views.py @@ -3,14 +3,15 @@ from django.http import HttpResponse + def index(request): now = datetime.now() - html = f''' + html = f"""

Hello from Vercel!

The current time is { now }.

- ''' - return HttpResponse(html) \ No newline at end of file + """ + return HttpResponse(html) diff --git a/python/django/manage.py b/python/django/manage.py index 8c45ccf303..923e331ade 100755 --- a/python/django/manage.py +++ b/python/django/manage.py @@ -1,12 +1,13 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" + import os import sys def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings') + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") try: from django.core.management import execute_from_command_line except ImportError as exc: @@ -18,5 +19,5 @@ def main(): execute_from_command_line(sys.argv) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/python/django/pyproject.toml b/python/django/pyproject.toml new file mode 100644 index 0000000000..d54c1f5d0d --- /dev/null +++ b/python/django/pyproject.toml @@ -0,0 +1,7 @@ +[project] +name = "django-vercel" +version = "0.1.0" +requires-python = ">=3.12" +dependencies = [ + "django>=6.0", +] diff --git a/python/django/requirements.txt b/python/django/requirements.txt deleted file mode 100644 index 68f14e4fe5..0000000000 --- a/python/django/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -Django==4.1.3 diff --git a/python/django/vercel.json b/python/django/vercel.json deleted file mode 100644 index d17e5ce0ff..0000000000 --- a/python/django/vercel.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "routes": [ - { - "src": "/(.*)", - "dest": "api/wsgi.py" - } - ] -} From ee9d3214b602ad4c73ec0f77e2620dc7177306b3 Mon Sep 17 00:00:00 2001 From: dnwpark Date: Fri, 13 Mar 2026 13:37:54 -0700 Subject: [PATCH 2/3] Add django-notes example. --- python/django-notes/.gitignore | 6 ++ python/django-notes/README.md | 90 +++++++++++++++++++ python/django-notes/app/__init__.py | 0 python/django-notes/app/settings.py | 70 +++++++++++++++ python/django-notes/app/urls.py | 5 ++ python/django-notes/app/wsgi.py | 16 ++++ python/django-notes/manage.py | 33 +++++++ python/django-notes/notes/__init__.py | 0 .../django-notes/notes/context_processors.py | 5 ++ python/django-notes/notes/forms.py | 18 ++++ .../notes/migrations/0001_initial.py | 27 ++++++ .../django-notes/notes/migrations/__init__.py | 0 python/django-notes/notes/models.py | 9 ++ .../django-notes/notes/static/notes/style.css | 89 ++++++++++++++++++ .../notes/templates/notes/base.html | 18 ++++ .../notes/templates/notes/detail.html | 24 +++++ .../notes/templates/notes/form.html | 32 +++++++ .../notes/templates/notes/list.html | 25 ++++++ python/django-notes/notes/urls.py | 11 +++ python/django-notes/notes/views.py | 53 +++++++++++ python/django-notes/pyproject.toml | 8 ++ 21 files changed, 539 insertions(+) create mode 100644 python/django-notes/.gitignore create mode 100644 python/django-notes/README.md create mode 100644 python/django-notes/app/__init__.py create mode 100644 python/django-notes/app/settings.py create mode 100644 python/django-notes/app/urls.py create mode 100644 python/django-notes/app/wsgi.py create mode 100644 python/django-notes/manage.py create mode 100644 python/django-notes/notes/__init__.py create mode 100644 python/django-notes/notes/context_processors.py create mode 100644 python/django-notes/notes/forms.py create mode 100644 python/django-notes/notes/migrations/0001_initial.py create mode 100644 python/django-notes/notes/migrations/__init__.py create mode 100644 python/django-notes/notes/models.py create mode 100644 python/django-notes/notes/static/notes/style.css create mode 100644 python/django-notes/notes/templates/notes/base.html create mode 100644 python/django-notes/notes/templates/notes/detail.html create mode 100644 python/django-notes/notes/templates/notes/form.html create mode 100644 python/django-notes/notes/templates/notes/list.html create mode 100644 python/django-notes/notes/urls.py create mode 100644 python/django-notes/notes/views.py create mode 100644 python/django-notes/pyproject.toml diff --git a/python/django-notes/.gitignore b/python/django-notes/.gitignore new file mode 100644 index 0000000000..84de335b5f --- /dev/null +++ b/python/django-notes/.gitignore @@ -0,0 +1,6 @@ +db.sqlite3 +__pycache__/ +*.pyc +.env +.env.local +.vercel diff --git a/python/django-notes/README.md b/python/django-notes/README.md new file mode 100644 index 0000000000..2fcceea36a --- /dev/null +++ b/python/django-notes/README.md @@ -0,0 +1,90 @@ +# Django Notes + +A simple note-taking app built with Django 6, demonstrating server-side rendering, URL routing, forms, and ORM with SQLite locally and Postgres on Vercel. + +## Demo + +https://django-notes.vercel.app/ + +## How it Works + +The `Note` model is defined in `notes/models.py`: + +```python +class Note(models.Model): + title = models.CharField(max_length=200) + body = models.TextField() +``` + +The database defaults to SQLite locally and switches to Postgres when `DATABASE_URL` is present — Vercel sets this automatically when you provision a Postgres database: + +```python +if os.environ.get("DATABASE_URL"): + # Postgres on Vercel +else: + DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", ...}} +``` + +CSS is served as a static file from `notes/static/notes/style.css` via Django's `staticfiles` app and referenced in the base template with `{% static %}`. + +The app is exposed to Vercel via WSGI in `app/wsgi.py`: + +```python +application = get_wsgi_application() +``` + +Setting the environment variable `READ_ONLY=true` disables all write operations and hides the create, edit, and delete UI. + +## Running Locally + +```bash +uv sync +uv run python manage.py migrate +uv run python manage.py runserver +``` + +Your app is now available at `http://localhost:8000`. + +## Deploying to Vercel + +This app uses SQLite locally. Vercel's serverless environment does not have a persistent filesystem, so you'll need to provision a Postgres database before deploying. + +**1. Install the Vercel CLI and link your project** + +```bash +npm install -g vercel +vercel link +``` + +**2. Add a Postgres database** + +From the [Vercel dashboard](https://vercel.com/dashboard), go to your project's **Storage** tab and create a Postgres database. Vercel will automatically add `DATABASE_URL` to your project's environment variables. + +**3. Pull environment variables and run migrations** + +```bash +vercel env pull +uv run python manage.py migrate +``` + +**4. Deploy** + +```bash +vercel --prod +``` + +## Project Structure + +``` +django-notes/ +├── app/ # Django project config (settings, urls, wsgi) +└── notes/ # Notes app + ├── models.py # Note model (title, body) + ├── forms.py # NoteForm + ├── views.py # list, create, detail, edit, delete views + ├── urls.py # URL patterns + ├── migrations/ # Database migrations + ├── static/notes/ # CSS + └── templates/notes/ # HTML templates +``` + diff --git a/python/django-notes/app/__init__.py b/python/django-notes/app/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/python/django-notes/app/settings.py b/python/django-notes/app/settings.py new file mode 100644 index 0000000000..e080ec1b54 --- /dev/null +++ b/python/django-notes/app/settings.py @@ -0,0 +1,70 @@ +import os +import urllib.parse +from pathlib import Path + +BASE_DIR = Path(__file__).resolve().parent.parent + +SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "django-insecure-change-me-in-production") + +DEBUG = True + +ALLOWED_HOSTS = ["127.0.0.1", "localhost", ".vercel.app"] + +READ_ONLY = os.environ.get("READ_ONLY", "").lower() in ("1", "true", "yes") + +INSTALLED_APPS = [ + "django.contrib.contenttypes", + "django.contrib.staticfiles", + "notes", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", +] + +ROOT_URLCONF = "app.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.request", + "notes.context_processors.read_only", + ], + }, + }, +] + +WSGI_APPLICATION = "app.wsgi.application" + +if os.environ.get("DATABASE_URL"): + url = urllib.parse.urlparse(os.environ["DATABASE_URL"]) + DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql", + "NAME": url.path.lstrip("/"), + "USER": url.username, + "PASSWORD": url.password, + "HOST": url.hostname, + "PORT": url.port, + } + } +else: + DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } + } + +LANGUAGE_CODE = "en-us" +TIME_ZONE = "UTC" +USE_I18N = True +USE_TZ = True + +STATIC_URL = "/static/" diff --git a/python/django-notes/app/urls.py b/python/django-notes/app/urls.py new file mode 100644 index 0000000000..450f38f8d0 --- /dev/null +++ b/python/django-notes/app/urls.py @@ -0,0 +1,5 @@ +from django.urls import path, include + +urlpatterns = [ + path("", include("notes.urls")), +] diff --git a/python/django-notes/app/wsgi.py b/python/django-notes/app/wsgi.py new file mode 100644 index 0000000000..373e747510 --- /dev/null +++ b/python/django-notes/app/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for app project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") + +application = get_wsgi_application() diff --git a/python/django-notes/manage.py b/python/django-notes/manage.py new file mode 100644 index 0000000000..b12f622a94 --- /dev/null +++ b/python/django-notes/manage.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" + +import os +import sys + +# Load .env.local if present (e.g. after running `vercel env pull`) +_env_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env.local") +if os.path.exists(_env_file): + with open(_env_file) as f: + for line in f: + line = line.strip() + if line and not line.startswith("#") and "=" in line: + key, _, value = line.partition("=") + os.environ.setdefault(key.strip(), value.strip().strip("\"'")) + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/python/django-notes/notes/__init__.py b/python/django-notes/notes/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/python/django-notes/notes/context_processors.py b/python/django-notes/notes/context_processors.py new file mode 100644 index 0000000000..c2249b0075 --- /dev/null +++ b/python/django-notes/notes/context_processors.py @@ -0,0 +1,5 @@ +from django.conf import settings + + +def read_only(request): + return {"read_only": settings.READ_ONLY} diff --git a/python/django-notes/notes/forms.py b/python/django-notes/notes/forms.py new file mode 100644 index 0000000000..7352cb0c01 --- /dev/null +++ b/python/django-notes/notes/forms.py @@ -0,0 +1,18 @@ +from django import forms +from .models import Note + + +class NoteForm(forms.ModelForm): + class Meta: + model = Note + fields = ["title", "body"] + widgets = { + "title": forms.TextInput(attrs={ + "placeholder": "Note title", + "autofocus": True, + }), + "body": forms.Textarea(attrs={ + "placeholder": "Write your note here...", + "rows": 8, + }), + } diff --git a/python/django-notes/notes/migrations/0001_initial.py b/python/django-notes/notes/migrations/0001_initial.py new file mode 100644 index 0000000000..2f6d645c9a --- /dev/null +++ b/python/django-notes/notes/migrations/0001_initial.py @@ -0,0 +1,27 @@ +from django.db import migrations, models + + +def seed_initial_note(apps, schema_editor): + Note = apps.get_model("notes", "Note") + Note.objects.create(title="Hello", body="world!") + Note.objects.create(title="Try it yourself", body="This demo is read-only. Deploy your own copy to create, edit, and delete notes.") + Note.objects.create(title="Django on Vercel", body="This app runs on Vercel Serverless Functions using the Python runtime.") + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [] + + operations = [ + migrations.CreateModel( + name="Note", + fields=[ + ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), + ("title", models.CharField(max_length=200)), + ("body", models.TextField()), + ], + ), + migrations.RunPython(seed_initial_note, migrations.RunPython.noop), + ] diff --git a/python/django-notes/notes/migrations/__init__.py b/python/django-notes/notes/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/python/django-notes/notes/models.py b/python/django-notes/notes/models.py new file mode 100644 index 0000000000..9fa268b00b --- /dev/null +++ b/python/django-notes/notes/models.py @@ -0,0 +1,9 @@ +from django.db import models + + +class Note(models.Model): + title = models.CharField(max_length=200) + body = models.TextField() + + def __str__(self): + return self.title diff --git a/python/django-notes/notes/static/notes/style.css b/python/django-notes/notes/static/notes/style.css new file mode 100644 index 0000000000..10cb700d8a --- /dev/null +++ b/python/django-notes/notes/static/notes/style.css @@ -0,0 +1,89 @@ +*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } + +body { + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; + background: #000; + color: #fff; + min-height: 100vh; +} + +header { + border-bottom: 1px solid #333; + padding: 1rem 2rem; + display: flex; + align-items: center; + justify-content: space-between; +} + +header a { color: #fff; text-decoration: none; font-weight: 600; } +header nav a { font-size: 0.875rem; color: #888; } +header nav a:hover { color: #fff; } + +main { max-width: 800px; margin: 0 auto; padding: 2rem; } + +h1 { font-size: 1.5rem; font-weight: 600; margin-bottom: 1.5rem; } + +.btn { + display: inline-block; + padding: 0.5rem 1rem; + border-radius: 6px; + font-size: 0.875rem; + font-weight: 500; + text-decoration: none; + cursor: pointer; + border: 1px solid #333; + background: #111; + color: #fff; +} +.btn:hover { background: #222; border-color: #555; } +.btn-primary { background: #fff; color: #000; border-color: #fff; } +.btn-primary:hover { background: #eee; } +.btn-danger { background: #c00; border-color: #c00; } +.btn-danger:hover { background: #a00; border-color: #a00; } + +.card { + background: #111; + border: 1px solid #333; + border-radius: 8px; + padding: 1.25rem; +} + +.note-grid { display: grid; gap: 1rem; } + +.note-card { + background: #111; + border: 1px solid #333; + border-radius: 8px; + padding: 1.25rem; + text-decoration: none; + color: inherit; + display: block; + transition: border-color 0.15s; +} +.note-card:hover { border-color: #555; } +.note-card h2 { font-size: 1rem; font-weight: 600; margin-bottom: 0.5rem; } +.note-card p { font-size: 0.875rem; color: #888; } + +.actions { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; align-items: center; } + +label { display: block; font-size: 0.875rem; color: #888; margin-bottom: 0.5rem; } + +input[type="text"], textarea { + width: 100%; + background: #111; + border: 1px solid #333; + border-radius: 6px; + padding: 0.625rem 0.75rem; + color: #fff; + font-size: 0.875rem; + font-family: inherit; + margin-bottom: 1rem; +} +input[type="text"]:focus, textarea:focus { + outline: none; + border-color: #555; +} + +.errorlist { color: #f55; font-size: 0.8rem; margin-bottom: 0.5rem; list-style: none; } + +.empty { color: #555; text-align: center; padding: 3rem 0; } diff --git a/python/django-notes/notes/templates/notes/base.html b/python/django-notes/notes/templates/notes/base.html new file mode 100644 index 0000000000..537e5e9e3e --- /dev/null +++ b/python/django-notes/notes/templates/notes/base.html @@ -0,0 +1,18 @@ +{% load static %} + + + + + + {% block title %}Django Notes{% endblock %} + + + +
+ Django Notes +
+
+ {% block content %}{% endblock %} +
+ + diff --git a/python/django-notes/notes/templates/notes/detail.html b/python/django-notes/notes/templates/notes/detail.html new file mode 100644 index 0000000000..cf5cf2adbd --- /dev/null +++ b/python/django-notes/notes/templates/notes/detail.html @@ -0,0 +1,24 @@ +{% extends "notes/base.html" %} + +{% block title %}{{ note.title }}{% endblock %} + +{% block content %} +
+

{{ note.title }}

+ {% if not read_only %} + Edit + {% endif %} +
+ +
+

{{ note.body }}

+
+ +{% if not read_only %} +
+ {% csrf_token %} + +
+{% endif %} +{% endblock %} diff --git a/python/django-notes/notes/templates/notes/form.html b/python/django-notes/notes/templates/notes/form.html new file mode 100644 index 0000000000..6a2524abec --- /dev/null +++ b/python/django-notes/notes/templates/notes/form.html @@ -0,0 +1,32 @@ +{% extends "notes/base.html" %} + +{% block title %}{{ action }} Note{% endblock %} + +{% block content %} +

{{ action }} Note

+ +
+ {% csrf_token %} + + {% if form.title.errors %} +
    {% for e in form.title.errors %}
  • {{ e }}
  • {% endfor %}
+ {% endif %} + + {{ form.title }} + + {% if form.body.errors %} +
    {% for e in form.body.errors %}
  • {{ e }}
  • {% endfor %}
+ {% endif %} + + {{ form.body }} + +
+ + {% if note %} + Cancel + {% else %} + Cancel + {% endif %} +
+
+{% endblock %} diff --git a/python/django-notes/notes/templates/notes/list.html b/python/django-notes/notes/templates/notes/list.html new file mode 100644 index 0000000000..183cdc8014 --- /dev/null +++ b/python/django-notes/notes/templates/notes/list.html @@ -0,0 +1,25 @@ +{% extends "notes/base.html" %} + +{% block title %}Notes{% endblock %} + +{% block content %} +
+

Notes

+ {% if not read_only %} + New Note + {% endif %} +
+ +{% if notes %} +
+ {% for note in notes %} + +

{{ note.title }}

+

{{ note.body|truncatechars:100 }}

+
+ {% endfor %} +
+{% else %} +

No notes yet.

+{% endif %} +{% endblock %} diff --git a/python/django-notes/notes/urls.py b/python/django-notes/notes/urls.py new file mode 100644 index 0000000000..19a4ada999 --- /dev/null +++ b/python/django-notes/notes/urls.py @@ -0,0 +1,11 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path("", views.note_list, name="note_list"), + path("notes/new/", views.note_create, name="note_create"), + path("notes//", views.note_detail, name="note_detail"), + path("notes//edit/", views.note_edit, name="note_edit"), + path("notes//delete/", views.note_delete, name="note_delete"), +] diff --git a/python/django-notes/notes/views.py b/python/django-notes/notes/views.py new file mode 100644 index 0000000000..edf2b977d6 --- /dev/null +++ b/python/django-notes/notes/views.py @@ -0,0 +1,53 @@ +from django.conf import settings +from django.http import HttpResponseForbidden +from django.shortcuts import get_object_or_404, redirect, render + +from .forms import NoteForm +from .models import Note + + +def note_list(request): + notes = Note.objects.all() + return render(request, "notes/list.html", {"notes": notes}) + + +def note_create(request): + if settings.READ_ONLY: + return HttpResponseForbidden("This demo is read-only.") + if request.method == "POST": + form = NoteForm(request.POST) + if form.is_valid(): + note = form.save() + return redirect("note_detail", pk=note.pk) + else: + form = NoteForm() + return render(request, "notes/form.html", {"form": form, "action": "Create"}) + + +def note_detail(request, pk): + note = get_object_or_404(Note, pk=pk) + return render(request, "notes/detail.html", {"note": note}) + + +def note_edit(request, pk): + if settings.READ_ONLY: + return HttpResponseForbidden("This demo is read-only.") + note = get_object_or_404(Note, pk=pk) + if request.method == "POST": + form = NoteForm(request.POST, instance=note) + if form.is_valid(): + form.save() + return redirect("note_detail", pk=note.pk) + else: + form = NoteForm(instance=note) + return render(request, "notes/form.html", {"form": form, "action": "Edit", "note": note}) + + +def note_delete(request, pk): + if settings.READ_ONLY: + return HttpResponseForbidden("This demo is read-only.") + note = get_object_or_404(Note, pk=pk) + if request.method == "POST": + note.delete() + return redirect("note_list") + return render(request, "notes/confirm_delete.html", {"note": note}) diff --git a/python/django-notes/pyproject.toml b/python/django-notes/pyproject.toml new file mode 100644 index 0000000000..25326652bc --- /dev/null +++ b/python/django-notes/pyproject.toml @@ -0,0 +1,8 @@ +[project] +name = "django-notes" +version = "0.1.0" +requires-python = ">=3.12" +dependencies = [ + "django>=6.0", + "psycopg[binary]>=3.1", +] From 3275178dd58d9bb4572699c7418e64b167b29382 Mon Sep 17 00:00:00 2001 From: dnwpark Date: Fri, 13 Mar 2026 15:46:17 -0700 Subject: [PATCH 3/3] Add django-rest-framework-example. --- python/django-rest-framework/.gitignore | 4 ++ python/django-rest-framework/README.md | 40 ++++++++++++++++++++ python/django-rest-framework/api/__init__.py | 0 python/django-rest-framework/api/urls.py | 7 ++++ python/django-rest-framework/api/views.py | 9 +++++ python/django-rest-framework/app/__init__.py | 0 python/django-rest-framework/app/settings.py | 33 ++++++++++++++++ python/django-rest-framework/app/urls.py | 5 +++ python/django-rest-framework/app/wsgi.py | 16 ++++++++ python/django-rest-framework/manage.py | 23 +++++++++++ python/django-rest-framework/pyproject.toml | 8 ++++ 11 files changed, 145 insertions(+) create mode 100644 python/django-rest-framework/.gitignore create mode 100644 python/django-rest-framework/README.md create mode 100644 python/django-rest-framework/api/__init__.py create mode 100644 python/django-rest-framework/api/urls.py create mode 100644 python/django-rest-framework/api/views.py create mode 100644 python/django-rest-framework/app/__init__.py create mode 100644 python/django-rest-framework/app/settings.py create mode 100644 python/django-rest-framework/app/urls.py create mode 100644 python/django-rest-framework/app/wsgi.py create mode 100644 python/django-rest-framework/manage.py create mode 100644 python/django-rest-framework/pyproject.toml diff --git a/python/django-rest-framework/.gitignore b/python/django-rest-framework/.gitignore new file mode 100644 index 0000000000..a98520dbb6 --- /dev/null +++ b/python/django-rest-framework/.gitignore @@ -0,0 +1,4 @@ +.venv/ +__pycache__/ +*.pyc +.env diff --git a/python/django-rest-framework/README.md b/python/django-rest-framework/README.md new file mode 100644 index 0000000000..115b644a68 --- /dev/null +++ b/python/django-rest-framework/README.md @@ -0,0 +1,40 @@ +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Fdjango-rest-framework&demo-title=Django%20REST%20Framework%20%2B%20Vercel&demo-description=A%20minimal%20Django%20REST%20Framework%20API%20running%20on%20Vercel%20Serverless%20Functions.&demo-url=https%3A%2F%2Fdjango-rest-framework.vercel.app%2F) + +# Django REST Framework + Vercel + +A minimal [Django REST Framework](https://www.django-rest-framework.org/) API running on Vercel with Serverless Functions using the [Python Runtime](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python). + +## Demo + +https://django-rest-framework.vercel.app/ + +## How it Works + +A single DRF view in `api/views.py` returns the current UTC time as JSON: + +```python +@api_view(["GET"]) +def current_time(request): + return Response({"time": datetime.now(timezone.utc).isoformat()}) +``` + +This view is exposed at `/api/time/` and the app is served via WSGI in `app/wsgi.py`: + +```python +application = get_wsgi_application() +``` + +## Running Locally + +```bash +uv sync +uv run python manage.py runserver +``` + +Your API is now available at `http://localhost:8000/api/time/`. + +## One-Click Deploy + +Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples): + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Fdjango-rest-framework&demo-title=Django%20REST%20Framework%20%2B%20Vercel&demo-description=A%20minimal%20Django%20REST%20Framework%20API%20running%20on%20Vercel%20Serverless%20Functions.&demo-url=https%3A%2F%2Fdjango-rest-framework.vercel.app%2F) diff --git a/python/django-rest-framework/api/__init__.py b/python/django-rest-framework/api/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/python/django-rest-framework/api/urls.py b/python/django-rest-framework/api/urls.py new file mode 100644 index 0000000000..36e5cbe2b8 --- /dev/null +++ b/python/django-rest-framework/api/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path("time/", views.current_time), +] diff --git a/python/django-rest-framework/api/views.py b/python/django-rest-framework/api/views.py new file mode 100644 index 0000000000..997dd97191 --- /dev/null +++ b/python/django-rest-framework/api/views.py @@ -0,0 +1,9 @@ +from datetime import datetime, timezone + +from rest_framework.decorators import api_view +from rest_framework.response import Response + + +@api_view(["GET"]) +def current_time(request): + return Response({"time": datetime.now(timezone.utc).isoformat()}) diff --git a/python/django-rest-framework/app/__init__.py b/python/django-rest-framework/app/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/python/django-rest-framework/app/settings.py b/python/django-rest-framework/app/settings.py new file mode 100644 index 0000000000..215c2d85ed --- /dev/null +++ b/python/django-rest-framework/app/settings.py @@ -0,0 +1,33 @@ +from pathlib import Path + +BASE_DIR = Path(__file__).resolve().parent.parent + +SECRET_KEY = "django-insecure-change-me-in-production" + +DEBUG = True + +ALLOWED_HOSTS = ["127.0.0.1", "localhost", ".vercel.app"] + +INSTALLED_APPS = [ + "django.contrib.contenttypes", + "rest_framework", + "api", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.middleware.common.CommonMiddleware", +] + +ROOT_URLCONF = "app.urls" + +WSGI_APPLICATION = "app.wsgi.application" + +DATABASES = {} + +REST_FRAMEWORK = { + "DEFAULT_AUTHENTICATION_CLASSES": [], + "DEFAULT_PERMISSION_CLASSES": [], + "DEFAULT_RENDERER_CLASSES": ["rest_framework.renderers.JSONRenderer"], + "UNAUTHENTICATED_USER": None, +} diff --git a/python/django-rest-framework/app/urls.py b/python/django-rest-framework/app/urls.py new file mode 100644 index 0000000000..9a63b9dfbe --- /dev/null +++ b/python/django-rest-framework/app/urls.py @@ -0,0 +1,5 @@ +from django.urls import path, include + +urlpatterns = [ + path("api/", include("api.urls")), +] diff --git a/python/django-rest-framework/app/wsgi.py b/python/django-rest-framework/app/wsgi.py new file mode 100644 index 0000000000..373e747510 --- /dev/null +++ b/python/django-rest-framework/app/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for app project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") + +application = get_wsgi_application() diff --git a/python/django-rest-framework/manage.py b/python/django-rest-framework/manage.py new file mode 100644 index 0000000000..923e331ade --- /dev/null +++ b/python/django-rest-framework/manage.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" + +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/python/django-rest-framework/pyproject.toml b/python/django-rest-framework/pyproject.toml new file mode 100644 index 0000000000..56f24b0b4e --- /dev/null +++ b/python/django-rest-framework/pyproject.toml @@ -0,0 +1,8 @@ +[project] +name = "django-rest-framework" +version = "0.1.0" +requires-python = ">=3.12" +dependencies = [ + "django>=6.0", + "djangorestframework>=3.15", +]