Guide

December 6, 2023

Launch a Django App

Build and deploy a Django app. This Template comes configured with a Postgres database

Overview

Python development and deployment on Noop should be familiar to anyone that’s leveraged containers for project isolation and local-cloud parity. For everyone else, it’s straighforward and has advantages over development directly on a developer host machine.

If you’re interested in starting a Django app from scratch, install Workshop and select the Python Django Template. The create-Template process in Workshop will automate everything outlined in this guide.

If, on the other hand, you want to modify an existing project to use Noop, the guide here can be used as reference.

The full source code constructed for this guide, including the Noop configuration, see the Template repository.

Setup

Noop dependencies

Make sure you have Workshop installed locally or a Github account connected to Noop Cloud. The remaining setup is identical in both.

Project setup

Download and install Python.

Create and activate a Python virtual environment

python -m venv venv
source venv/bin/activate

Install Django and other requirements from this repo’s requirements.txt

pip install -r requirements.txt

Bootstrap a Django project

django-admin startproject noop_example

The complete Python Django Template and project source code for the example created in this guide can be found on Github.

Template Configuration

After you have the Django project set up, you need to add the Noop Template configuration to create a local or cloud Deployment.

Create a .noop directory in your project root.

mkdir .noop

Next create a blueprint.yaml file to define the application setup.

The content below is the necessary config for our app:

components:
  - name: Django
    type: service
    image: python:3.12.0-slim
    port: 8000
    build:
      steps:
        - directory: /app
        - copy: requirements.txt
          destination: ./
        - copy: noop_example
          destination: noop_example
        - run: pip3 install -r requirements.txt --no-cache-dir
    runtime:
      command: python3 noop_example/manage.py runserver 0.0.0.0:8000
      resources:
        - DjangoDB
      variables:
        DB_NAME:
          $resources: DjangoDB.database
        DB_USER:
          $resources: DjangoDB.username
        DB_PASS:
          $resources: DjangoDB.password
        DB_HOST:
          $resources: DjangoDB.host
        DB_PORT:
          $resources: DjangoDB.port
  - name: DjangoMigrate
    type: task
    image: python:3.12.0-slim
    build:
      steps:
        - directory: /app
        - copy: requirements.txt
          destination: ./
        - copy: noop_example
          destination: noop_example
        - run: pip3 install -r requirements.txt --no-cache-dir
    runtime:
      command: python3 noop_example/manage.py migrate
      resources:
        - DjangoDB
      variables:
        DB_NAME:
          $resources: DjangoDB.database
        DB_USER:
          $resources: DjangoDB.username
        DB_PASS:
          $resources: DjangoDB.password
        DB_HOST:
          $resources: DjangoDB.host
        DB_PORT:
          $resources: DjangoDB.port
lifecycles:
  - event: BeforeServices
    components:
    - DjangoMigrate
resources:
  - name: DjangoDB
    type: postgresql
routes:
  - target:
      component: Django

Update noop_example/noop_example/settings.py to read the database bindings from the environment variables defined in .noop/app.yml and allow noop hostnames.

import environ

env = environ.Env()
environ.Env.read_env()

# ...

ALLOWED_HOSTS = ['.noop.app']

# ...

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': env('DB_NAME'),
        'USER': env('DB_USER'),
        'PASSWORD': env('DB_PASS'),
        'HOST': env('DB_HOST'),
        'PORT': env('DB_PORT')
    }
}

# ...

In order to automate most of the Application setup, including Environment creation, database provisioning, building and deploying, etc., we’re going to create a Runbook. Create a file called quickstart.yml in the .noop/runbooks directory.

Here is the Runbook content:

name: Quickstart Setup

description: Demo full stack setup of this Django app with internet endpoint

workflow:
  inputs:
    - name: EnvironmentName
      description: What should we name your new environment?
      type: string
      required: true
      default: Quickstart Demo

    - name: Cluster
      type: Cluster
      description: Which cluster should the environment launch on?
      required: true

  timeout: 300

  steps:
    - name: Environment
      action: EnvironmentCreate
      params:
        name:
          $inputs: EnvironmentName
        production: false
        appId:
          $runbook: Application.id
        clusterId:
          $inputs: Cluster.id

    - name: Build
      action: BuildExecute
      params:
        sourceCodeId:
          $runbook: SourceCode.id
        appId:
          $runbook: Application.id

    - name: Resources
      action: ResourceLaunch
      params:
        envId:
          $steps: Environment.id
        sourceCodeId:
          $runbook: SourceCode.id

    - name: Deploy
      action: DeploymentExecute
      params:
        envId:
          $steps: Environment.id
        buildId:
          $steps: Build.id

    - name: Endpoint
      action: InternetEndpointRandom
      params:
        orgId:
          $runbook: Organization.id
        routes:
          - name: 'Demo Environment'
            target:
              environments:
                - $steps: Environment.id

Launch on Noop

There are a few slight differences between launching on Workshop and Cloud. Details for each are outlined below.

Create an Application

Select “New Application” and assign an appropriate name for your new app.

Cloud Setup

In Cloud, once the Application is created you will be asked if you want to execute a Runbook. The name of the Runbook for this Template is “Quick Start”.

Choose the default cluster and run it. Behind the scenes this Runbook is:

  • Creating an Environment
  • Setting up placeholders for required environment variables
  • Launching the database
  • Building the source code
  • Deploying the build
  • And finally, creating a public-facing endpoint to serve the application from

Once complete, visit the Application page to get the generated endpoint. Your Django app is now running at that URL.

The Noop Template Blueprint contains all the details about the application. Including build process, required resources, application routing rules, runtime configuration, etc.

Workshop Setup

While workshop doesn’t have a Runbook, the setup is also simple.

Select your Application, which should have been automatically detected by Workshop, or select the appropriate project directory (must include a .noop directory).

Workshop will automatically create a default environment with all Resources launched.

Once done, you should see your application running on the associated endpoint.


This guide was written with:

SoftwareVersion
Noopv0.5.3
Pythonv3.12.0
Djangov4.2.7