Note
Before you start, check out the Upsun demo app and the main Getting started guide. They provide all of the core concepts and common commands you need to know before using the materials below.1. Leverage environment variables
Yoursettings.py file may allow for environment variables to be set for common pieces of configuration.
In this case, add and commit a .environment file that includes those details.
.environment
2. Configure ALLOWED_HOSTS
By default, other than localhost, Django only allows hosts listed in settings.ALLOWED_HOSTS to be accessed. However, Django does not allow for wildcard hosts that span multiple levels by default. This becomes relevant in order to support our dynamic preview environments you will want to dynamically add to the list.
The simplest method is to add the following line to .environment :
DJANGO_ALLOWED_HOSTS environment variable.
3. Upsun-specific settings
Near the bottom of yoursettings.py file, define a block that:
- Detects when Django is running on an Upsun environment
- Override previous settings
production.py file for production settings, place it there instead.
settings.py
-
Overwrites. If the
PLATFORM_APPLICATION_NAMEUpsun built-in variable is found (that is, Django is running on an Upsun environment), override your previous settings. No matter what environment type we run on Upsun, this file uses production settings for Upsun (i.e.DEBUG = False). -
Static.
STATIC_ROOT, and thestaticfiles path is updated relative to the application root on Upsun. -
Secret key. All Upsun projects come with a unique hash environment variable
PLATFORM_PROJECT_ENTROPYthat can be used to update yourSECRET_KEY. -
Databases. When Django is running on an Upsun enviroment at runtime, it has access to service containers like databases and caches.
Every service container you configure in
.upsun/config.yamlhas a unique relationship name (applications:<APP_NAME>:relationships:<RELATIONSHIPNAME>). Upsun automatically uses this relationship name to expose connection credentials through environment variables (for example, viaRELATIONSHIPNAME_HOST).
Updatesettings.pyaccording to the example above (which configures a PostgreSQL service), where the relationshipdatabaseresults in environment variables that are leveraged to update theDATABASESsetting for your application.
You can use the exact same logic to configureCACHESfrom therediscacherelationship using the exposedREDISCACHE_environment variables to setupdjango_redis.cache.RedisCache.
4. Start the app
In your app configuration, locate theweb:commands:start section and update it as follows:
Note that if your Django instance requires a different web server,
Upsun also supports several other options.
5. Configure static assets
To access Django’s static assets, you need to add a second location to theweb:locations section of your app configuration.
Locate the web:locations section and add a location for /static:
6. Install dependencies and builds
Instruct Upsun to install your Python and Node (if needed) dependencies. Locate thehooks:build section and update it as follows:
Remove the npm steps if not required for your app’s assets.
Note that if your project uses a different package manager,
Upsun also supports several other options.
7. Configure the deploy phase
In your app configuration, locate thedeploy section and update it as follows:
8. Allow write access where needed
Since Django can require a writable locations at runtime, you need to set up writable mounts. To do so, locate themounts section (currently commented), and update it as follows:
You can now commit all of the above changes and push to Upsun.
Terminal