Giter VIP home page Giter VIP logo

hello-django-postgres's Introduction

HelloDjangoPostgres!

This is an example project demonstrating how to deploy a Django application with Postgres on Fly.io.

Local Development

The environment variables are stored in the .env file.

Duplicate .env.dist file and rename it to .env. Update the environment variables:

SECRET_KEY=<your-generated-django-secure-super-secret-key>
DEBUG=True
DATABASE_URL=postgres://<user>:<password>@<host>:<port>/<db>

The default DATABASE_URL is postgres://postgres:postgres@localhost:5432/hello_django (check hello_django/settings.py).


Getting started

flyctl is the command-line utility provided by Fly.io.

If you still don't have installed it, you can follow the instructions here to install it, sign up and log in to Fly.io.


Create and configure a new app (fly launch)

This simple app already contains all the basic configuration for deploying to Fly.io.

  • Dockerfile contain commands to build the image.
  • .dockerignore list of files or directories Docker will ignore during the build process.
  • fly.toml configuration for deployment on Fly.io.

Dockerfile

SECRET_KEY is required when running collectstatic. A default SECRET_KEY can be generated by using the get_random_secret_key function provided by Django:

# settings.py
from django.core.management.utils import get_random_secret_key
...
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str('SECRET_KEY', default=get_random_secret_key())

An alternative is to set a random key on Dockerfile for building purposes:

# Dockerfile
ENV SECRET_KEY "non-secret-key-for-building-purposes"  # <-- Set SECRET_KEY for building purposes
RUN python manage.py collectstatic --noinput

When running fly launch, copy the existing configuration to your own app:

❯ fly launch
An existing fly.toml file was found for app hello-django-postgres
? Would you like to copy its configuration to the new app? Yes

Secrets

During the fly launch, the necessary secrets will be set:

  • SECRET_KEY
  • DATABASE_URL

Django SECRET_KEY

❯ fly launch
Creating app in /Projects/flyio/hello-django-postgres
Scanning source code
Detected a Django app
? Choose an app name (leave blank to generate one): hello-django-postgres
...
Hostname: hello-django-postgres.fly.dev
Set secrets on hello-django-postgres: SECRET_KEY  # <-- SECRET_KEY is set automatically

Postgres DATABASE_URL

Set up a Postgres database during the fly launch step:

❯ fly launch
Creating app in /Projects/flyio/hello-django-postgres
Scanning source code
Detected a Django app
? Choose an app name (leave blank to generate one): hello-django-postgres
...
? Would you like to set up a Postgresql database now? Yes  # <-- You will be asked HERE
? Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
Creating postgres cluster in organization fly-io
Creating app...
Setting secrets on app hello-django-postgres-db...
Provisioning 1 of 1 machines with image flyio/postgres:14.6
Waiting for machine to start...
....
Postgres cluster hello-django-postgres-db is now attached to hello-django-postgres
The following secret was added to hello-django-postgres:
  DATABASE_URL=postgres://hello_django_postgres:****@****:5432/hello_django_postgres?sslmode=disable
Postgres cluster hello-django-postgres-db is now attached to hello-django-postgres  # <-- postgres cluster attached to the app

Important: During this step, save your credentials in a secure place - you won't be able to see them again!

Your postgres cluster will be automatically attached to your app.

ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS

Before we can deploy it, make sure to update ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS in hello_django/settings.py with the chosen/generated app name on Fly.io.

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '<your-app-name>.fly.dev']
CSRF_TRUSTED_ORIGINS = ['https://<your-app-name>.fly.dev']

Your Django app is now ready to be deployed!


Deploy (fly deploy)

Once all the previous steps are done, you can deploy your app:

❯ fly deploy
==> Verifying app config
--> Verified app config
==> Building image
...
 1 desired, 1 placed, 1 healthy, 0 unhealthy [health checks: 1 total, 1 passing]
--> v0 deployed successfully

Your app is now up and running!

❯ fly open

hello-django-postgres's People

Contributors

katiayn avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

initnirvana

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.