[](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 and 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).
Version 1.0.0
## Demo5
https://django-template.vercel.app/
## How it Works
Our Django application, `example` is configured as an installed application in `vercel_app/settings.py`:
Our Django application, `example` is configured as an installed application settings.py`:
```python
# vercel_app/settings.py
# settings.py
INSTALLED_APPS = [
# ...
'django_app',
]
```
We allow "\*.vercel.app" subdomains in `ALLOWED_HOSTS`, in addition to 127.0.0.1:
We allow subdomains in `ALLOWED_HOSTS`, in addition to 127.0.0.1:
```python
# vercel_app/settings.py
ALLOWED_HOSTS = ['127.0.0.1', '.vercel.app']
# settings.py
ALLOWED_HOSTS = ['127.0.0.1']
```
The `wsgi` module must use a public variable named `app` to expose the WSGI application:
```python
# vercel_app/wsgi.py
# wsgi.py
app = get_wsgi_application()
```
The corresponding `WSGI_APPLICATION` setting is configured to use the `app` variable from the `vercel_app.wsgi` module:
The corresponding `WSGI_APPLICATION` setting is configured to use the `beckend'` variable from the module:
```python
# vercel_app/settings.py
WSGI_APPLICATION = 'vercel_app.wsgi.app'
WSGI_APPLICATION = 'vercel_app.wsgi.beckend'
```
There is a single view which renders the current time in `example/views.py`:
@ -47,10 +36,8 @@ There is a single view which renders the current time in `example/views.py`:
```python
# django_app/views.py
from datetime import datetime
from django.http import HttpResponse
def index(request):
now = datetime.now()
html = f'''
@ -69,7 +56,6 @@ This view is exposed a URL through `example/urls.py`:
```python
# django_app/urls.py
from django.urls import path
from django_app.views import index
urlpatterns = [
@ -77,7 +63,7 @@ urlpatterns = [
]
```
Finally, it's made accessible to the Django server inside `vercel_app/urls.py`:
Finally, it's made accessible to the Django server inside `urls.py`:
```python
# vercel_app/urls.py
@ -96,28 +82,17 @@ This example uses the Web Server Gateway Interface (WSGI) with Django to enable
```bash
python manage.py collectstatic
python manage.py runserver 8080
python manage.py runserver 8090
docker rmi -f <image_id>
docker compose down && docker compose build --no-cache && docker compose up
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):
[](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)
Your Django application is now available at `http://localhost:8090`.