Skip to content

About

Django and PostgreSQL starter that deploys on Cosmoner.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Django on Cosmoner

Deploy to Cosmoner

A small Django app backed by PostgreSQL, ready to run as a web service on Cosmoner. It has the Django admin enabled, a Note model to manage in it, and a page that lists the notes.

  • config/settings.py reads everything that differs between environments from environment variables.
  • core/ holds the model, its migration, the views and the admin.
  • GET /health answers 200 when the app and the database both respond.

What happens on deploy

The app is built automatically from pyproject.toml and uv.lock. Each time it starts, bin/start.sh (named in the Procfile):

  1. collects static files, which WhiteNoise then serves, compressed and with cache-busting names;
  2. runs python manage.py migrate;
  3. creates the first admin account, if DJANGO_SUPERUSER_USERNAME and DJANGO_SUPERUSER_PASSWORD are set and that account does not exist yet;
  4. starts Gunicorn on the port in the PORT environment variable, which Cosmoner sets for you.

Deploy

This app needs a PostgreSQL database and a DATABASE_URL secret before it can start.

  1. In your Cosmoner project, create a PostgreSQL database.
  2. Store its connection string as a secret named DATABASE_URL. Use the internal host, which apps in the same project connect to.
  3. Select the button above, copy the template to your GitHub account, and deploy your copy. The wizard can fill in every setting from .cosmoner/deployment.yaml, and asks you for two values:
Variable What to enter
DJANGO_SECRET_KEY A long random string, e.g. the output of python -c "import secrets; print(secrets.token_urlsafe(50))"
DJANGO_SUPERUSER_PASSWORD The password for the admin account

Once the app is live, sign in at /admin/ as admin. The account is only created when it does not exist, so changing DJANGO_SUPERUSER_PASSWORD later does not change the password; do that in the admin.

Optional variables:

Variable Default
DJANGO_ALLOWED_HOSTS .cosmoner.com,localhost,127.0.0.1. Add your custom domain here, comma-separated.
DJANGO_CSRF_TRUSTED_ORIGINS Empty. Only needed for forms posted from another origin.
WEB_CONCURRENCY 2 Gunicorn workers.

Run locally

Requires Python 3.13, uv and a PostgreSQL database.

uv sync
export DATABASE_URL=postgresql://user:password@localhost:5432/app
export DJANGO_DEBUG=true
uv run python manage.py migrate
uv run python manage.py createsuperuser
uv run python manage.py runserver

Open http://localhost:8000, and the admin at http://localhost:8000/admin/.

After changing a model, run uv run python manage.py makemigrations and commit the new migration; it is applied on the next deploy.

License

MIT

About

Django and PostgreSQL starter that deploys on Cosmoner.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages