Django Deployments#

If this is your first time deploying to CodeRed Cloud, it is recommended to read our Django Quickstart, and familiarize yourself with our Django Environment.

First, be sure to install the cr command line tool.

Run your Django project locally#

Before deploying, your Django project should be running locally without errors, and should be up-to-date with all migrations. Django provides some commands to help with this:

$ python manage.py check
$ python manage.py makemigrations check

If either of the commands returns any output, resolve the output before continuing.

Check your Django settings and project files#

CodeRed Cloud relies on four particular files to work correctly:

  • requirements.txt - Python packages.

  • manage.py - Django management command.

  • project/wsgi.py - Django WSGI file.

  • project/settings/prod.py - Django settings.

The cr command line tool provides a command which will check and fix these for you (replace WEBAPP with your webapp handle):

$ cr check WEBAPP

If any of the files are missing, and you’re not sure how to proceed, contact our support and we’ll be happy to help.

Note

requirements.txt is a conventional way of specifying which pip packages to install for your project. Generally this should be hand-edited to include your direct dependencies, with versions pinned. For example:

Django==4.1.*
requests==2.*

This particular example says to install the latest security release of Django 4.1, and the latest verion of requests 2.

It is NOT recommended to use the contents of pip freeze as your requirements.txt, because this contains mostly developer tooling that is not needed on the server, and can make your app slower and more vulnerable to security breaches! Dumping pip freeze also makes it extremely difficult to update a single package in the future, known as “dependency hell”.

If you have development dependencies, such as django_debug_toolbar, put those in a requirements-dev.txt file, which will be ignored by the production server. An example requirements-dev.txt file:

# Install production requirements.
-r requirements.txt

# Install developer tooling.
django-debug-toolbar
flake8
black

Pre-deployment steps#

If you have any build steps, such as building a Node.js front-end, or Sass/CSS build, you should perform those locally using the production profile. If the outputted files of these tools are ignored by version control, be sure to flag them for inclusion in the deployment using the deploy_include setting for the cr tool (or manually copy them via SFTP).

Deploy#

At this point, you are ready to deploy your app. During the deployment process, the CodeRed host server performs the following automatically:

  • Cycles database passwords and RANDOM_SECRET_KEY environment variables using cryptographic functions.

  • Pulls in the latest platform and Python security updates.

  • Installs packages from requirements.txt

  • Runs manage.py migrate

  • Runs manage.py collectstatic

  • Issues or re-issues your SSL certificate.

Deploy using the cr tool:

$ cr deploy WEBAPP

This will copy the files, initiate a deployment, then stream the deployment log to the console. If you see any critical errors in the output, call our support line to quickly resolve.

Automating Deployments#

Deployments can be automated on git push using GitHub, GitLab, Azure DevOps, or other continuous integration tools. Follow our automation guide.

Tips and FAQs#

Do I need whitenoise or other static file serving tools? No! CodeRed Cloud automatically finds your static files, and serves them from the filesystem using a fast native web server. Shims such as whitenoise are not needed.

Do I need object storage, such as S3, for media? No! While you can use 3rd-party object storage, it is not required. CodeRed Cloud automatically finds your media files, and serves them from the filesystem using a fast native web server. These files are also included in your daily backups.

Can I use poetry or pipenv with CodeRed Cloud? Yes, as long as you generate a requirements.txt before running the deployment.