Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jincao1/django-hello-world
https://github.com/jincao1/django-hello-world
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/jincao1/django-hello-world
- Owner: jincao1
- Created: 2024-04-22T23:38:23.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-04-23T01:00:44.000Z (9 months ago)
- Last Synced: 2024-04-24T00:41:45.079Z (9 months ago)
- Language: Python
- Homepage: https://django-hello-world-rosy-eight.vercel.app
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![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)
# 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).
## Demo
https://django-template.vercel.app/
## How it Works
Our Django application, `example` is configured as an installed application in `api/settings.py`:
```python
# api/settings.py
INSTALLED_APPS = [
# ...
'example',
]
```We allow "\*.vercel.app" subdomains in `ALLOWED_HOSTS`, in addition to 127.0.0.1:
```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'
```There is a single view which renders the current time in `example/views.py`:
```python
# example/views.py
from datetime import datetimefrom django.http import HttpResponse
def index(request):
now = datetime.now()
html = f'''
Hello from Vercel!
The current time is { now }.
'''
return HttpResponse(html)
```This view is exposed a URL through `example/urls.py`:
```python
# example/urls.py
from django.urls import pathfrom example.views import index
urlpatterns = [
path('', index),
]
```Finally, it's made accessible to the Django server inside `api/urls.py`:
```python
# api/urls.py
from django.urls import path, includeurlpatterns = [
...
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
```bash
python manage.py runserver
```Your Django application is now available at `http://localhost:8000`.
## 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&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)