Application settings
Before you can deploy your app, you need to update yoursettings.py file so your app can run properly in both development and production environments.
If you don’t already have a preferred approach to handle your settings, you can use django-environ. You can install django-environ with your package manager.
.env in your project’s base directory. At a minimum, .env needs to include the following variables:
- DEBUG
- SECRET_KEY
- ALLOWED_HOSTS
- CSRF_TRUSTED_ORIGINS
- DATABASE_URL
.env will only contain your development values.
.env file, you need to update your settings.py file so django-environ can read .env and set the appropriate settings. DEBUG should default to False, so your production environment never starts in debug mode.
env.list will convert comma-separated values to a Python list. If those values don’t exist, you should default to an empty list, so you have no allowed hosts or CSRF trusted origins.
env.db() on the default database; this loads the DATABASE_URL value defined in your environment variables.
Add a hosted database
The filesystem used for your app will be recreated on each deploy, so an SQLite database isn’t suitable for production. Instead, you can use a hosted database on Sevalla for your app. Create a database in Sevalla and select either Postgres, MySQL, or MariaDB. You also need to install a database driver if you haven’t already done so. For example, with Postgres, you can usepsycopg2-binary.
Application server and static files
Django’s built-in development server is not intended for production use, so you should run your application with Gunicorn instead. Because the development server also handles static files by default, you’ll need to use Whitenoise to serve static assets in production. Both Gunicorn and Whitenoise must be installed via your package manager as part of your deployment setup.settings.py. This middleware should be placed after the SecurityMiddleware.
STATIC_ROOT variable in settings.py.
Nixpacks
By default, Sevalla builds your application using Nixpacks. Nixpacks can detect that you have a Python app, but you need to specify any extra commands needed to build your app. You also need a start command to tell Nixpacks how to start your app. You can define both commands in anixpacks.toml file in your base directory.
For the build command, you need to run collectstatic. For the start command, you need to run both the migrate and gunicorn command. After the gunicorn command, specify the name of your Django project followed by .wsgi. The $PORT variable can be used to start your app on the port defined in your Sevalla application.
requirements.txt file so your dependencies install during deployment.
Dockerfile
To build your application from a Dockerfile, you need a Dockerfile that ends with your application being started by Gunicorn or any other WSGI server. The following is a sample Dockerfile that sets up a Python environment, installs your dependencies, collects static files, and then starts a Gunicorn server.ARG directive is used to make the associated environment variables available to the build process. These environment variables should be set as available at both buildtime and runtime before deploying your app for the first time.
The command to start with Gunicorn needs to reference the name of your project’s WSGI file.
The rest of the Dockerfile can be customized according to your project’s specific needs.
Migrations
Migrations need to happen outside the Dockerfile because there won’t be a reference to your actual database in the Dockerfile. Additionally, your migrations must run after your image has been built but before the container is started. You can create a job to run the migrate command before the container is started. To do this, after you add your application, go to Processes > Create job > Job. For the start command, addpython manage.py migrate. The start policy should be before deployment, and the smallest instance size should be sufficient for migrations.
Build settings and deploy
By default, Sevalla builds applications using Nixpacks, so the build strategy must be updated before your Dockerfile can be used. To update the build strategy, after you add your application, go to Settings > Update build strategy and change the build strategy from Nixpacks to Dockerfile.Deploy on Sevalla
To deploy your app to Sevalla using Git, your code must be hosted in a Git repository. Sevalla supports any public Git repository or private repositories from GitHub, Bitbucket, and GitLab. You’ll need to connect your repo host account with Sevalla if you are using a private repo. Your repo should have a.gitignore that ignores SQLite files, .env files, virtual environments, __pycache__/, any other files that either have sensitive information or don’t need to be tracked in git.
Here is an example .gitignore file:
.env except for DATABASE_URL. The values for the variables should be appropriate for production.
ALLOWED_HOSTS: include only the domain with no scheme or trailing slash. (e.g. deploydjangosevalla-4q4g3.sevalla.app).CSRF_TRUSTED_ORGINS: include the scheme but no trailing slash. (e.g. https://deploydjangosevalla-4q4g3.sevalla.app).DEBUG: should always be false except for intentional testing.SECRET_KEY: should be a random string of at least 50 characters.
DATABASE_URL, go to Networking and click Add internal connection. Select the database you created earlier, and then select Add environment variables to the application. Rename DB_URL to DATABASE_URL and click Add internal connection.