GeezerVison is showing github.com — Add Python examples. (#469) · daaniyaan/examples@d1143f1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +.vercel | | 2 | +*.log | | 3 | +*.pyc | | 4 | +__pycache__ | | 5 | +db.sqlite3 | | 6 | +media | | 7 | + | | 8 | +# Environments | | 9 | +.env | | 10 | +.venv | | 11 | +env/ | | 12 | +venv/ | | 13 | +ENV/ | | 14 | +env.bak/ | | 15 | +venv.bak/ |
Collapse file Copy file name to clipboard +85Lines changed: 85 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +[](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/examples/tree/main/python/django) | | 2 | + | | 3 | +# Django + Vercel | | 4 | + | | 5 | +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). | | 6 | + | | 7 | +## Demo | | 8 | + | | 9 | +https://django-template.vercel.app/ | | 10 | + | | 11 | +## How it Works | | 12 | + | | 13 | +Our Django application, `example` is configured as an installed application in `vercel_app/settings.py`: | | 14 | + | | 15 | +```python | | 16 | +# vercel_app/settings.py | | 17 | +INSTALLED_APPS = [ | | 18 | + # ... | | 19 | + 'example', | | 20 | +] | | 21 | +``` | | 22 | + | | 23 | +There is a single view which renders the current time in `example/views.py`: | | 24 | + | | 25 | +```python | | 26 | +# example/views.py | | 27 | +from datetime import datetime | | 28 | + | | 29 | +from django.http import HttpResponse | | 30 | + | | 31 | + | | 32 | +def index(request): | | 33 | + now = datetime.now() | | 34 | + html = f''' | | 35 | + <html> | | 36 | + <body> | | 37 | + <h1>Hello from Vercel!</h1> | | 38 | + <p>The current time is { now }.</p> | | 39 | + </body> | | 40 | + </html> | | 41 | + ''' | | 42 | + return HttpResponse(html) | | 43 | +``` | | 44 | + | | 45 | +This view is exposed a URL through `example/urls.py`: | | 46 | + | | 47 | +```python | | 48 | +# example/urls.py | | 49 | +from django.urls import path | | 50 | + | | 51 | +from example.views import index | | 52 | + | | 53 | + | | 54 | +urlpatterns = [ | | 55 | + path('', index), | | 56 | +] | | 57 | +``` | | 58 | + | | 59 | +Finally, it's made accessible to the Django server inside `vercel_app/urls.py`: | | 60 | + | | 61 | +```python | | 62 | +# vercel_app/urls.py | | 63 | +from django.urls import path, include | | 64 | + | | 65 | +urlpatterns = [ | | 66 | + ... | | 67 | + path('', include('example.urls')), | | 68 | +] | | 69 | +``` | | 70 | + | | 71 | +This example uses the Web Server Gateway Interface (WSGI) with Django to enable handling requests on Vercel with Serverless Functions. | | 72 | + | | 73 | +## Running Locally | | 74 | + | | 75 | +```bash | | 76 | +python manage.py runserver | | 77 | +``` | | 78 | + | | 79 | +Your Django application is now available at `http://localhost:8000`. | | 80 | + | | 81 | +## One-Click Deploy | | 82 | + | | 83 | +Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples): | | 84 | + | | 85 | +[](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/examples/tree/main/python/django) |
Collapse file Copy file name to clipboardExpand all lines: python/django/example/__init__.py Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from django.contrib import admin | | 2 | + | | 3 | +# Register your models here. |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from django.apps import AppConfig | | 2 | + | | 3 | + | | 4 | +class ExampleConfig(AppConfig): | | 5 | + default_auto_field = 'django.db.models.BigAutoField' | | 6 | + name = 'example' |
Collapse file Copy file name to clipboard +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# example/urls.py | | 2 | +from django.urls import path | | 3 | + | | 4 | +from example.views import index | | 5 | + | | 6 | + | | 7 | +urlpatterns = [ | | 8 | + path('', index), | | 9 | +] |
Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# example/views.py | | 2 | +from datetime import datetime | | 3 | + | | 4 | +from django.http import HttpResponse | | 5 | + | | 6 | +def index(request): | | 7 | + now = datetime.now() | | 8 | + html = f''' | | 9 | + <html> | | 10 | + <body> | | 11 | + <h1>Hello from Vercel!</h1> | | 12 | + <p>The current time is { now }.</p> | | 13 | + </body> | | 14 | + </html> | | 15 | + ''' | | 16 | + return HttpResponse(html) |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#!/usr/bin/env python | | 2 | +"""Django's command-line utility for administrative tasks.""" | | 3 | +import os | | 4 | +import sys | | 5 | + | | 6 | + | | 7 | +def main(): | | 8 | + """Run administrative tasks.""" | | 9 | + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'vercel_app.settings') | | 10 | + try: | | 11 | + from django.core.management import execute_from_command_line | | 12 | + except ImportError as exc: | | 13 | + raise ImportError( | | 14 | + "Couldn't import Django. Are you sure it's installed and " | | 15 | + "available on your PYTHONPATH environment variable? Did you " | | 16 | + "forget to activate a virtual environment?" | | 17 | + ) from exc | | 18 | + execute_from_command_line(sys.argv) | | 19 | + | | 20 | + | | 21 | +if __name__ == '__main__': | | 22 | + main() |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +Django==4.1.3 |
Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "builds": [ | | 3 | + { | | 4 | + "src": "vercel_app/wsgi.py", | | 5 | + "use": "@vercel/python" | | 6 | + } | | 7 | + ], | | 8 | + "routes": [ | | 9 | + { | | 10 | + "src": "/(.*)", | | 11 | + "dest": "vercel_app/wsgi.py" | | 12 | + } | | 13 | + ] | | 14 | +} |
|