Deploy Django Wagtail to Render
Deploy Django Wagtail to Render
Project Demo
Render:
https://wagtail-render-pm23.onrender.com/admin/
https://wagtail-react-backend.onrender.com/admin/
Username: admin
Password: password
Gitlab:
https://gitlab.com/stackingtabs/wagtail-react.git
https://gitlab.com/stabstest1/wagtail-render.git
Youtube:
https://youtu.be/Po1sAdKnJrY
A Push Away from Heroku
After 12.5 hours of straight deploy failure in a 20-hour unpaid ‘coding practice’ day, I now share with you my happiness: I got a green circle after 9 reds, on my last attempt for the day.
Heroku announced, “Starting November 28th, 2022, free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis® will no longer be available.” As a self-taught who hasn’t applied for her first coding job yet, this was a blow, as I am also not qualified for their proposed plan for students.
I began to look for Heroku alternatives, that have $0 tier. I uploaded the list here, on the same day I found out I will need to take a chunk out of my savings for Heroku. There are a couple dozen free alternatives, I settled on Render for Django Wagtail.
https://www.youtube.com/watch?v=v3q81irqtLM
Having gone through the deployment on Netlify for React frontend and Heroku for Django Wagtail backend (I was just starting to record a whole video course for it on my Youtube), I began to find my way through Render. Only a few links, that you can refer to:
https://render.com/docs/deploy-django
https://render.com/blog/migrate-django-from-heroku
https://render.com/docs/environment-variables
https://github.com/render-examples/django/
https://github.com/render-examples/django-quick-start
https://www.swyx.io/django-on-render
Since the tutorial by Render is for Django + PostgreSQL and I use only SQLite, Django’s default database, this post may not apply depending on the database you choose to use.
The Way Render Renders
Render -> Github & Gitlab
Private Repo -> Gitlab
Unlike Heroku, where you can manually upload your repository, you need to use either Github or Gitlab for Render. Great, I have Github. How about, private repository? No, I do not have Gitlab. Finally my time to try, as I have always wanted to, so I can hide my half-baked code from nobody’s eyes but my own.
For Wagtail, choose Web Service> Docker.
I followed the docs from https://render.com/docs/deploy-django. You can see my Gitlab code. There is an example or two on Github by Render too, although they are dated.
Skipped the PostgreSQL. Did
python -m pip install gunicorn psycopg2 whitenoise
For the static files configuration, I did not use the ‘if not DEBUG: …’ from the docs because it caused me an error, so I left it as it is.
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"
For build script, I did not follow the docs but modified a little from https://github.com/render-examples/django-quick-start/blob/master/build.sh :
Build.sh
#!/usr/bin/env bash
pip install -r requirements.txt
python manage.py migrate
python manage.py collectstatic
Even if I had the wrong ‘pip install -r requirements/production.txt’ in build.sh, it still works. Odd.
To fill requirements.txt, run
python -m pip freeze > requirements.txt
To compile static files, run
python manage.py collectstatic --noinput --clear
Render.yaml
services:
- type: web
name: wagtail-react
env: python
plan: starter
buildCommand: ./build.sh
startCommand: gunicorn mysite.wsgi:application
envVars:
- key: WEB_CONCURRENCY
value: 4
- key: DATABASE_URL
fromDatabase:
name: wagtail-react
property: connectionString
# - key: DJANGO_SECRET_KEY
- key: SECRET_KEY
generateValue: true
- key: DJANGO_ADMIN_URL
generateValue: true
- key: DJANGO_SETTINGS_MODULE
value: mysite.settings.productiondatabases:
- name: wagtail-react
databaseName: django
user: django
No credit card required? A̶t̶ ̶f̶i̶r̶s̶t̶,̶ ̶t̶h̶e̶n̶ ̶b̶o̶o̶m̶ ̶y̶o̶u̶ ̶n̶e̶e̶d̶ ̶o̶n̶e̶.̶ No credit card, I will show you how.
It feels great going through the whole process on Render without needing a credit card. Some people do not have one, some are afraid of incurring charges due to unintentional overuse, some fear for safety.
Spoiler:
A̶f̶t̶e̶r̶ ̶d̶e̶p̶l̶o̶y̶m̶e̶n̶t̶,̶ ̶y̶o̶u̶ ̶n̶o̶w̶ ̶n̶e̶e̶d̶ ̶t̶o̶ ̶p̶a̶y̶ ̶t̶o̶ ̶c̶r̶e̶a̶t̶e̶s̶u̶p̶e̶r̶u̶s̶e̶r̶,̶ ̶u̶s̶i̶n̶g̶ ̶R̶e̶n̶d̶e̶r̶ ̶s̶h̶e̶l̶l̶,̶ ̶a̶n̶d̶ ̶s̶h̶e̶l̶l̶ ̶i̶s̶ ̶p̶a̶i̶d̶.̶
B̶u̶m̶m̶e̶r̶,̶ ̶b̶u̶t̶ ̶c̶o̶n̶t̶i̶n̶u̶e̶ ̶o̶n̶ ̶w̶i̶t̶h̶ ̶t̶h̶e̶ ̶t̶u̶t̶o̶r̶i̶a̶l̶-̶t̶u̶r̶n̶e̶d̶-̶d̶i̶s̶a̶p̶p̶o̶i̶n̶t̶m̶e̶n̶t̶ ̶i̶f̶ ̶y̶o̶u̶ ̶w̶a̶n̶t̶.̶ ̶S̶i̶n̶c̶e̶ ̶I̶ ̶w̶a̶s̶t̶e̶d̶ ̶m̶y̶ ̶t̶i̶m̶e̶,̶ ̶I̶ ̶m̶i̶g̶h̶t̶ ̶a̶s̶ ̶w̶e̶l̶l̶ ̶w̶a̶s̶t̶e̶ ̶s̶o̶m̶e̶ ̶m̶o̶r̶e̶ ̶t̶o̶ ̶d̶o̶c̶u̶m̶e̶n̶t̶ ̶m̶y̶ ̶o̶w̶n̶ ̶e̶x̶p̶e̶r̶i̶e̶n̶c̶e̶.̶
I approached the staff chat to verify that it is indeed not free to deploy Django. While waiting for their reply, I searched for my own answer. Again, StackOverflow comes to the rescue. Apparently, I do not need that shell. Yay. Now how?
The approach is familiar, it was something I read on before, but never tried myself, until today.
I sorted by newest, and pick the most feasible looking approach, calling on my ‘hunch’. Result was ‘deploy live’ (the green button) but ‘service failed’ / ‘server unhealthy’. No, it was not Render’s server that was unhealthy, it was my code.
Ironically, the older answer from 2018 was spot on, at least for mine.
A minor change:
f̶r̶o̶m̶ ̶d̶j̶a̶n̶g̶o̶.̶c̶o̶n̶t̶r̶i̶b̶.̶a̶u̶t̶h̶.̶m̶o̶d̶e̶l̶s̶ ̶i̶m̶p̶o̶r̶t̶ ̶u̶s̶e̶r̶
from django.contrib.auth import get_user_model
A minor change: from django.contrib.auth.models import user to django.contrib.auth import get_user_model. The reason is here: https://docs.djangoproject.com/en/4.0/ref/django-admin/#createsuperuser
Looks like this:
import osfrom django.contrib.auth import get_user_model
from django.core.wsgi import get_wsgi_applicationos.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.dev")application = get_wsgi_application()User = get_user_model();User.objects.create_superuser('admin', 'admin@admin.com', 'password')
If you use local server to run ‘py manage.py runserver
’, you will get an error, particularly because you have already created a superuser on your local machine.
It works on Render.
git add .
git commit -m "yay for free Wagtail CMS"
git push
Ta-da.
This is my barebone, quick, as-long-as-it-works approach. I hope with your kind feedback, I can improve.