Introduction
Get started with Django on Google Axion C4A
Configure firewall rules for Django on Google Cloud
Create a Google Axion C4A Arm virtual machine on GCP
Install Django on your Arm-based VM
Verify Django installation and run the development server
Deploy Django on GKE Axion with managed data services
Build a Django REST API with PostgreSQL and Redis
Containerize and deploy Django on Axion GKE
Benchmark Django application performance on Arm
Next Steps
In this section you’ll build a Django REST API that uses PostgreSQL for database storage and Redis for caching. This matches how production applications are typically structured.
Set up a clean Python virtual environment and install all runtime dependencies required for Django, PostgreSQL, Redis, and production serving with Gunicorn.
mkdir ~/django_api && cd ~/django_api
python3 -m venv venv
source venv/bin/activate
sudo zypper install postgresql-devel libpq5 postgresql15-server-devel gcc make python3-devel
Install the required Python packages:
which pg_config
pip install psycopg2-binary django djangorestframework django-redis gunicorn
Create the Django project structure:
django-admin startproject django_api .
django-admin startapp api
The Django project structure is ready with all required libraries for database, cache, and API support.
Enable the REST framework and API app by editing django_api/settings.py. Add ‘rest_framework’ and ‘api’ to INSTALLED_APPS, set DEBUG to False, and add ‘*’ to ALLOWED_HOSTS:
DEBUG = False
ALLOWED_HOSTS = ["*"]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api',
]
Django is configured to run as an API server.
This step connects Django to external managed services instead of local SQLite. This mirrors how real production systems operate.
Edit django_api/settings.py and replace the DATABASES configuration with this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'django_db',
'USER': 'django_user',
'PASSWORD': 'password',
'HOST': 'CLOUDSQL_IP',
'PORT': '5432',
}
}
Replace CLOUDSQL_IP with the actual Cloud SQL IP address you saved earlier.
Additionally, add the CACHES configuration per below to the same file and save:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://REDIS_IP:6379/1",
}
}
Edit the above addition and set REDIS_IP to the actual IP address that you saved earlier.
The application is configured to use PostgreSQL and Redis.
Django creates tables, metadata, and user models inside PostgreSQL.
python manage.py migrate
python manage.py createsuperuser
The PostgreSQL instance contains all Django system tables.
A health endpoint allows Kubernetes and load balancers to verify if the service is running.
Add code below in api/views.py file:
from rest_framework.response import Response
from rest_framework.decorators import api_view
@api_view(["GET"])
def health(request):
return Response({"status":"ok"})
Add code below in django_api/urls.py file:
from django.urls import path
from api.views import health
urlpatterns = [
path("healthz/", health),
]
The health endpoint is configured.
Before containerizing or deploying, validate that everything works end-to-end.
python manage.py runserver 0.0.0.0:8000
In a separate SSH session, run:
curl http://127.0.0.1:8000/healthz/
The expected output is:
{"status":"ok"}
The Django API is functional with PostgreSQL and Redis connected.
In this section, you built a cloud-ready Django REST API that:
Next, you’ll containerize this application and deploy it to your Axion-powered GKE cluster.