18
loading...
This website collects cookies to deliver better user experience
python3 -m venv ~/.venv/django-app
django-app
in the ~/.venv
directory. The virtualenv contains a copy of the Python interpreter, pip, the standard library, and various supporting files.source ~/.venv/django-app/bin/activate
mkdir django-on-koyeb
cd django-on-koyeb
mysqlclient requires system librairies, please check
https://pypi.org/project/mysqlclient/ for detailled instructions
pip install django gunicorn dj_database_url mysqlclient
pip freeze > requirements.txt
django-admin startproject django_on_koyeb ./
django_on_koyeb
directory in your current directory containing the following files:├── django_on_koyeb
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
settings.py
file as we need to customize settingssettings.py
using your favorite editor, here nano:nano django_on_koyeb/settings.py
settings.py
file to load some important settings of our application using environment variables.os
module.+import os
from pathlib import Path
SECRET_KEY
. This key is used to provide cryptographic signing and should be set to a unique, unpredictable value.from django.core.management.utils import get_random_secret_key
:import os
+from django.core.management.utils import get_random_secret_key
from pathlib import Path
SECRET_KEY = ...
line in the settings.py
file and replace it as below:SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', get_random_secret_key())
DJANGO_SECRET_KEY
environment variable is set, our application will use it. If no value is provided, the get_random_secret_key()
function is called and will return a random secret key.DEBUG
. When deploying the application in production, for security and performance purposes DEBUG
mode needs to be disabled. We will change the default value to disable DEBUG until it is explicitly enabled via the environment variable DJANGO_DEBUG=True
.DEBUG = os.environ.get('DJANGO_DEBUG', 'False') == 'True'
ALLOWED_HOSTS
directive to read the DJANGO_ALLOWED_HOSTS
from the environment. ALLOWED_HOSTS
is required to contain the list of strings representing the host/domain names that the Django application can serve.ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "localhost,127.0.0.1").split(",")
STATIC_URL
:STATIC_URL = '/static/'
+STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
pscale database create django-on-koyeb
pscale password create django-on-koyeb main production-password
pscale branch create django-on-koyeb migration
pscale password create django-on-koyeb migration migration-branch-password
custom_db_backends
directory in your Django application root folder:git clone https://github.com/vitessio/vitess.git ~/vitess
cp -r ~/vitess/support/django/custom_db_backends ./
settings.py
:nano django_on_koyeb/settings.py
settings.py
to use a Vitess database and retrieve the configuration via the DJANGO_DATABASE_URL
environment variable.DJANGO_DATABASE_URL
is not set, the application will raise the exception DJANGO_DATABASE_URL environment variable not defined
when starting.settings.py
replace the following part:# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
if os.getenv("DJANGO_DATABASE_URL", None) is None:
raise Exception("DJANGO_DATABASE_URL environment variable not defined")
DB_PARAMS = dj_database_url.parse(os.environ.get("DJANGO_DATABASE_URL"))
DB_PARAMS["ENGINE"] = "custom_db_backends.vitess"
DATABASES = {
"default": DB_PARAMS,
}
dj_database_url
module we're using:import os
from django.core.management.utils import get_random_secret_key
+import dj_database_url
from pathlib import Path
settings.py
. Let's run Django migration to propagate the application models to the PlanetScale database. In the terminal run the following command:DJANGO_DATABASE_URL=mysql://PS_USERNAME:PS_PASSWORD@PS_HOST:3306 python manage.py migrate
PS_USERNAME
, PS_PASSWORD
, PS_HOST
values with the PlanetScale values you generated for the migration
branch.migration
branch to the production branch main
and deploy it. You can also perform this operation using the PlanetScale CLI by running:pscale deploy-request create django-on-koyeb migration
pscale deploy-request deploy django-on-koyeb 1
git init
.gitignore
file to exclude undesired files.curl https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore > .gitignore
git add .
git commit -m "Django app initial commit"
django-on-koyeb
and click the Create repository button.git remote add origin [email protected]:YOUR_GITHUB_USERNAME/django-on-koyeb.git
git branch -M main
git push -u origin main
main
as the branch value.gunicorn --worker-tmp-dir /dev/shm django_on_koyeb.wsgi
DJANGO_DATABASE_URL
as the value and in the value section click Create Secret and name your Secret django-db-url
and mysql://PS_USERNAME:PS_PASSWORD@PS_HOST:3306
as value. Take care to replace PS_USERNAME, PS_PASSWORD, and PS_HOST with the PlanetScale password information you created for your main production branch.DJANGO_DEBUG
and value set to True
. We need this to validate our application is running properly as it doesn't contain any routes at the moment.DJANGO_ALLOWED_HOSTS
and value set to django-on-koyeb-<KOYEB-ORG>.koyeb.app
django-on-koyeb
and click the Create App button.https://django-on-koyeb-<KOYEB-ORG>.koyeb.app
.