Learn how to deploy your Django application on Google App Engine, integrate it with Cloud SQL, and map it to a custom domain in this comprehensive tutorial. I'll guide you step-by-step through the entire process, from setting up your Google Cloud environment to configuring your Django app for production. Whether you're a beginner or an experienced developer, this video will help you get your Django project up and running smoothly on Google Cloud Platform.
Refer youtube Video for this project
- Build and Test Django App in Local
- Setting up Google App Engine
- Deploy Django App on Google App Engine
- Connecting Django to Cloud SQL (MySQL)
- Mapping a custom domain to your app
![image](https://private-user-images.githubusercontent.com/143475073/336846565-1ecd1e87-cb7f-4ebf-b0cc-dcda3979be3f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwMzIxNzgsIm5iZiI6MTcyNDAzMTg3OCwicGF0aCI6Ii8xNDM0NzUwNzMvMzM2ODQ2NTY1LTFlY2QxZTg3LWNiN2YtNGViZi1iMGNjLWRjZGEzOTc5YmUzZi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwMTQ0MzhaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mZmM2NThmYmUwNmFkM2ViZTdjNGFhMmZmZGQ2YjY5NDA2ZTE0ZDczYTNmZTM5ZGUyNDM0N2U4ZDcwMmE2NzQ2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.TETSKkJ8khUWmT5nwC8rMtbfJFePw62RDVGteJQzNr4)
-
Install Django:
pip install django
-
Create a New Django Project:
django-admin startproject notes_project cd notes_project
-
Start the Development Server:
python manage.py runserver
-
Create a New Django App:
python manage.py startapp notes
-
Add the App to Installed Apps: Edit
notes_project/settings.py
and add'notes'
to theINSTALLED_APPS
list:INSTALLED_APPS = [ ... 'notes', ]
-
Create the Note Model: Edit
notes/models.py
to define the Note model:from django.db import models class Note(models.Model): title = models.CharField(max_length=200) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title
-
Create and Apply Migrations:
python manage.py makemigrations python manage.py migrate
- Create Views for Listing, Creating, and Viewing Notes:
Edit
notes/views.py
to add the necessary views:from django.shortcuts import render, get_object_or_404, redirect from .models import Note from .forms import NoteForm def note_list(request): notes = Note.objects.all() return render(request, 'notes/note_list.html', {'notes': notes}) def note_detail(request, pk): note = get_object_or_404(Note, pk=pk) return render(request, 'notes/note_detail.html', {'note': note}) def note_create(request): if request.method == 'POST': form = NoteForm(request.POST) if form.is_valid(): form.save() return redirect('note_list') else: form = NoteForm() return render(request, 'notes/note_form.html', {'form': form})
- Create a Form for the Note Model:
Create
notes/forms.py
:from django import forms from .models import Note class NoteForm(forms.ModelForm): class Meta: model = Note fields = ['title', 'content']
-
Define URLs for the Notes App: Create
notes/urls.py
:from django.urls import path from . import views urlpatterns = [ path('', views.note_list, name='note_list'), path('<int:pk>/', views.note_detail, name='note_detail'), path('new/', views.note_create, name='note_create'), ]
-
Include Notes URLs in the Project URLs: Edit
notes_project/urls.py
:from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('notes/', include('notes.urls')), ]
-
Create Templates Directory and Templates: Create a directory
notes/templates/notes/
and add the following HTML files:-
note_list.html
:<!DOCTYPE html> <html> <head> <title>{{ note.title }}</title> <style> body {font-family: Arial, sans-serif;} .header {background-color: #362264;color: white;padding: 5px;text-align: center;} .container {padding: 20px;} form {margin-top: 20px;} </style> </head> <body> <div class="header"> <h1>TechTrapture Notes</h1> </div> <h1>Notes</h1> <ul> {% for note in notes %} <li><a href="{% url 'note_detail' note.pk %}">{{ note.title }}</a></li> {% endfor %} </ul> <a href="{% url 'note_create' %}">Add a new note</a> </body> </html>
-
note_detail.html
:<!DOCTYPE html> <html> <head> <title>{{ note.title }}</title> <style> body {font-family: Arial, sans-serif;} .header {background-color: #362264;color: white;padding: 5px;text-align: center;} .container {padding: 20px;} form {margin-top: 20px;} </style> </head> <body> <div class="header"> <h1>TechTrapture Notes</h1> </div> <h1>{{ note.title }}</h1> <p>{{ note.content }}</p> <p><a href="{% url 'note_list' %}">Back to Notes</a></p> </body> </html>
-
note_form.html
:<!DOCTYPE html> <html> <head> <title>{{ note.title }}</title> <style> body {font-family: Arial, sans-serif;} .header {background-color: #362264;color: white;padding: 5px;text-align: center;} .container {padding: 20px;} form {margin-top: 20px;} </style> </head> <body> <div class="header"> <h1>TechTrapture Notes</h1> </div> <h1>New Note</h1> <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Save</button> </form> </body> </html>
-
-
Start the Development Server:
python manage.py runserver
-
Access the Application: Open your browser and navigate to
http://127.0.0.1:8000/notes/
to see your notes application in action.
You've now created a simple notes application with Django that allows you to list, view, and create notes. This basic setup can be expanded with additional features like updating and deleting notes, user authentication, and more advanced styling with CSS frameworks like Bootstrap.
To deploy your Django application on Google App Engine , follow these steps:
-
Create CloudSQL MySQL Instance Ensure you have CloudSQL MySQL Instance created and one database for application is created.
-
Update
settings.py
: Add the necessary configurations for static files and allowed hosts.DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': '/cloudsql/tt-dev-001:us-central1:dev', 'PORT' : '3306', 'USER': 'root', 'PASSWORD': 'mysql', 'NAME': 'appdb', } }
-
Create
requirements.txt
: Generate arequirements.txt
file for the dependencies.Django mysqlclient
-
Download and install the Cloud SQL Auth Proxy to your local machine. Right-click https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.11.3/cloud-sql-proxy.x64.exe and select Save Link As to download the Cloud SQL Auth Proxy. Rename the file to cloud-sql-proxy.exe. In seperate terminal run Cloud SQL Auth Proxy
./cloud-sql-proxy PROJECT_ID:REGION:INSTANCE_NAME
-
Run the Django migrations to set up your models on New Database i.e Cloud SQL.
python manage.py makemigrations
python manage.py migrate
Now run and Test Application locally
Start the Development Server:
python manage.py runserver
Access the Application:
Open your browser and navigate to http://127.0.0.1:8000/notes/
to see your notes application in action.
- Create
app.yaml
: Create anapp.yaml
file in your project's root directory.runtime: python39 service: noteapp
- Create
main.py
: Add the necessary configurations for static files and allowed hosts.from notes_project.wsgi import application app = application
-
Install Google Cloud SDK: If you haven’t already, install the Google Cloud SDK by following the installation guide.
-
Initialize Your Google Cloud Project:
gcloud init
-
Authenticate Your Google Cloud Account:
gcloud auth login
-
Set the Project:
gcloud config set project YOUR_PROJECT_ID
-
Deploy Your Application: Deploy your application to Google App Engine.
gcloud app deploy
-
Browse Your Application: Open your browser to view the deployed application.
gcloud app browse
To Map custom Domain Use Below Link - Mapping custom Domain to Google App Engine
This guide walks you through setting up a Django application for deployment on Google App Engine. It includes steps for configuring your Django project, preparing it for deployment, and deploying it using the Google Cloud SDK. By following these steps, you can ensure your application is ready for a scalable and reliable deployment on Google App Engine.
![image](https://private-user-images.githubusercontent.com/143475073/336817157-79ade334-5e0d-4dc3-b312-1ae25acf11e4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwMzIxNzgsIm5iZiI6MTcyNDAzMTg3OCwicGF0aCI6Ii8xNDM0NzUwNzMvMzM2ODE3MTU3LTc5YWRlMzM0LTVlMGQtNGRjMy1iMzEyLTFhZTI1YWNmMTFlNC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwMTQ0MzhaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lMjg3YjQyZTExZWZlZmJhNzQ3MDQxOWZiMjAzMjg0OGY1ZGUwYjRkODI1OGI1MmZlNjRiMWM1MDI0ZTQyNGYwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.4qRtiIuHvQV2un2PJCvxx0w9AWpG6-fbZF-_oCp7Hbk)