You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: How To Deploy a Django App to Render.com
slug: django-on-render
category: tutorial
tags: ['Python', Deploying, Django, Render.com]
date: 2020-02-07
description: Messing around learning Django and deploying
I pottered around a bit with Django today, blasting through the tutorial. However the deployment section was rather impenetrable for someone who doesn't even know what WSGI stands for.
I figured I should try deploying it to Render.com to learn both.
Table of Contents
TL;DR
Steps
The official Render tutorial for Django uses PostGres, which means the web server and database takes up your two free deployments. It also had issues when I tried it. So I wanted to try a "from scratch" Django project, from the official tutorial, using SQLite.
Going through the whole tutorial will get you something like https://github.com/sw-yx/django-quick-start. I modified mysite a bit to add a nicer landing page. Commit it to a GitHub repo.
Make sure to configure the necessary commands and env vars:
DJANGO_SECRET_KEY: something strong. can generate with echo "$(openssl rand -base64 32)"
Build command: ./build.sh - this script, not from the tutorial, installs Python's requirements.txt - i'm not totally sure if this is needed, I copied it from https://github.com/render-examples/django-quick-start/ so I assume it is
Start command: cd mysite && gunicorn mysite.wsgi:application. Note the cd mysite is just because of the filestructure of my project I had set up. A "professional" project would presumably be a little flatter. You will note the weird syntax of mysite.wsgi:application - application is the variable where the WSGI callable is stored.
I had previously tried and failed to use Render's Django Quick Start repo, so if you had DJANGO_SETTINGS_MODULE set to config.settings.production, Django will be looking for a file that doesn't exist and fail nastily. Delete it.
IAAC is important for scaling/reproducability, and a nice one click deploy experience. The docs aren't fully fleshed out yet but fortunately there is a decent sample YAML file published. I was also able to find plenty of examples by searching GitHub.
services:
- type: webname: djangotutorialenv: pythonbuildCommand: "./build.sh"# ensure it's a stringstartCommand: cd mysite && gunicorn mysite.wsgi:applicationrepo: https://github.com/sw-yx/django-quick-start.git # optional# plan: standard # optionalhealthCheckPath: /# autoDeploy: false # optionalenvVars:
- key: DJANGO_SECRET_KEYgenerateValue: true # will generate a base64-encoded 256-bit secret
You'll observe that all static assets fail to load in production, despite it working in local development. I reckon this is some misconfiguration of the static assets finding that I did. The official example has other static file finding strategies i have yet to explore.
Misc
I made this dump of Render's env vars, which I figured I could use in my application code:
title: How To Deploy a Django App to Render.com
slug: django-on-render
category: tutorial
tags: ['Python', Deploying, Django, Render.com]
date: 2020-02-07
description: Messing around learning Django and deploying
I pottered around a bit with Django today, blasting through the tutorial. However the deployment section was rather impenetrable for someone who doesn't even know what WSGI stands for.
I figured I should try deploying it to Render.com to learn both.
Table of Contents
TL;DR
Steps
The official Render tutorial for Django uses PostGres, which means the web server and database takes up your two free deployments. It also had issues when I tried it. So I wanted to try a "from scratch" Django project, from the official tutorial, using SQLite.
mysite
a bit to add a nicer landing page. Commit it to a GitHub repo.DJANGO_SECRET_KEY
: something strong. can generate withecho "$(openssl rand -base64 32)"
./build.sh
- this script, not from the tutorial, installs Python'srequirements.txt
- i'm not totally sure if this is needed, I copied it from https://github.com/render-examples/django-quick-start/ so I assume it iscd mysite && gunicorn mysite.wsgi:application
. Note thecd mysite
is just because of the filestructure of my project I had set up. A "professional" project would presumably be a little flatter. You will note the weird syntax ofmysite.wsgi:application
-application
is the variable where the WSGI callable is stored.I had previously tried and failed to use Render's Django Quick Start repo, so if you had
DJANGO_SETTINGS_MODULE
set toconfig.settings.production
, Django will be looking for a file that doesn't exist and fail nastily. Delete it.With that, your Django app should be up and running. Mine is deployed at: https://django-test-9g3f.onrender.com/ and you can see the stateful voting app in action at https://django-test-9g3f.onrender.com/polls. https://django-test-9g3f.onrender.com/admin also works.
Render.yaml
IAAC is important for scaling/reproducability, and a nice one click deploy experience. The docs aren't fully fleshed out yet but fortunately there is a decent sample YAML file published. I was also able to find plenty of examples by searching GitHub.
Most "Deploy to Render" buttons take the referer url to determine which repo to deploy, but you can customize this by just linking directly to the dashboard with the right path variables e.g. https://dashboard.render.com/iac/new?repoOwner=sw-yx&repoName=django-quick-start&branch=master&provider=GITHUB
Failures and Todos
You'll observe that all static assets fail to load in production, despite it working in local development. I reckon this is some misconfiguration of the static assets finding that I did. The official example has other static file finding strategies i have yet to explore.
Misc
I made this dump of Render's env vars, which I figured I could use in my application code:
The text was updated successfully, but these errors were encountered: