Deploying Django 4.2 on Render.com

Original link: https://www.ixiqin.com/2023/06/26/deploying-django-4-2-on-render-com/

1a90a582a46267af9e5701b8bc63ef34.jpg

When I was writing a translation tool for Linux China recently, I used Django as the backend, and I chose Django 4.2 and Python 3.11 for the version. When deploying Django, I chose to use Render.com to deploy. However, when deploying, I encountered some problems. The official Getting Started with Django on Render provided by Render will be deployed incorrectly, so with today’s article, I will tell you how to deploy the latest Django 4.2 to Render.

Initialize the project

Render does not use pip, but uses Poetry to manage the Django project, so you need to use Poetry to complete the project initialization.

 poetry init #初始化Poetry 的配置文件poetry add django gunicorn # 添加依赖Django 和gunicorn poetry run django-admin startproject linuxondjango .

The initialization project is basically replacing pip with Poetry. There is no part that needs to be specialized for Render here, so I won’t introduce too much.

write logic code

After you have completed the initialization of the project, you can write your own business logic code. I won’t talk about this part more, and it can be developed and used normally.

Configure the project to support Render’s server-side environment.

1. Read the Secret Key from the environment variable

Django uses Secret Key as the Salt and Seed for some encryption scenarios such as Session encryption, so when Django Admin creates a project, a Session will be generated by default. However, for security reasons, it is best not to put it in the code, but to store it through the environment variable after the server is generated, so as to avoid the session being decrypted after the code is leaked.

You need to add the following code in settings.py to replace the default key.

 import os # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ.get('SECRET_KEY', default='your secret key')

2. Read the Debug configuration in the environment variable

Render will automatically configure some environment variables. Therefore, you can directly determine whether you are currently on the server side of Render by judging the current environment context. If not, configure Debug to True to solve the requirement of not using Debug mode online.

 DEBUG = 'RENDER' not in os.environ

3. Read available domain names from environment variables

Django is configured with a domain name. Without a configured domain name, the current application cannot be accessed. Therefore, you need to read the domain name in Render to ensure normal access. Of course, if you configure your own domain name, you can also manually write it in ALLOWED_HOSTS .

 ALLOWED_HOSTS = [] RENDER_EXTERNAL_HOSTNAME = os.environ.get("RENDER_EXTERNAL_HOSTNAME") if RENDER_EXTERNAL_HOSTNAME: ALLOWED_HOSTS.append(RENDER_EXTERNAL_HOSTNAME)

Configure render.yml to support Render BluePrint

You can directly copy the following content as the startup configuration of your project. Where build.sh is the configuration for building the project.

build.sh

The most important thing in build.sh is to reinstall Poetry, because I am using Python 3.11.4, which does not match the default Python 3.7 of Render, so there is no way to use the default Poetry directly, and Poetry needs to be automatically and manually upgraded.

 #!/usr/bin/env bash # exit on error set -o errexit pip install --upgrade pip; pip install poetry; # 重新安装一下最新的Poetry,因为默认的Poetry 的版本比较低。 poetry install python manage.py collectstatic --no-input python manage.py migrate

render.yml

Among Render, the most important are startCommand and PYTHON_VERSION . startCommand here is that I use gunicorn to start the Django application, and PYTHON_VERSION is used to set the specific Python version. Here I choose Python 3.11.4 according to my own needs.

 databases: - name: linuxondjango-db databaseName: mysite user: mysite plan: free services: - type: web name: linuxondjango plan: free runtime: python buildCommand: "./build.sh" startCommand: "gunicorn linuxondjango.wsgi:application" envVars: - key: DATABASE_URL fromDatabase: name: linuxondjango-db property: connectionString - key: SECRET_KEY generateValue: true - key: WEB_CONCURRENCY value: 4 - key: PYTHON_VERSION # 这里的python version 是用来指定Python 版本的,比如这里我用的是3.11.4。 value: 3.11.4

Summarize

Render’s tutorial is generally not a big problem, but in some small points, you need to hack it yourself, for example, you need to upgrade Poetry and set the Python version. If you are also using a higher version of Django & Render, I hope this article can help you.

This article is transferred from: https://www.ixiqin.com/2023/06/26/deploying-django-4-2-on-render-com/
This site is only for collection, and the copyright belongs to the original author.