diff --git a/samples/django-postgres/README.md b/samples/django-postgres/README.md index 551ed064..e7472a3d 100644 --- a/samples/django-postgres/README.md +++ b/samples/django-postgres/README.md @@ -4,9 +4,6 @@ This template is a customer relationship management list project developed using Python Django framework, offering a starting point to help you quickly build your customer management system. We use PostgreSQL as the database. We have prepared all the essential files for deployment. By spending less than 10 minutes setting up the environment, as detailed in the prerequisites, and executing the commands in our step-by-step guide, your website will be ready to go live to the world! -> [!NOTE] -This sample showcases how you could deploy a full-stack application with Defang and Django. However, it deploys Postgres as a Defang service. Defang [services](https://12factor.net/processes) are ephemeral and should not be used to run stateful workloads in production as they will be reset on every deployment. For production use cases you should use a managed database like RDS, Aiven, or others. If you stick to Django's default SQLite database, your stored data will be lost on every deployment, and in some other situations. In the future, Defang will help you provision and connect to managed databases. - ## Prerequisites 1. Download [Defang CLI](https://github.com/DefangLabs/defang) @@ -28,10 +25,29 @@ For this sample, you will need to provide the following [configuration](https:// > Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you. ### `POSTGRES_PASSWORD` + +The password for the PostgreSQL database. + ```bash defang config set POSTGRES_PASSWORD ``` +### `SECRET_KEY` + +The secret key is used to secure the Django application. + +```bash +defang config set SECRET_KEY +``` + +### `ALLOWED_HOSTS` + +The allowed hosts for the Django application. (i.e. the domain your app runs on) + +```bash +defang config set ALLOWED_HOSTS +``` + ## Deployment > [!NOTE] diff --git a/samples/django-postgres/app/crm_platform/settings.py b/samples/django-postgres/app/crm_platform/settings.py index dcd912bd..8795d490 100644 --- a/samples/django-postgres/app/crm_platform/settings.py +++ b/samples/django-postgres/app/crm_platform/settings.py @@ -21,17 +21,15 @@ # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-^0jq%7b(%aj$j@n0_$gk@#73&z#t%4o#klquddg1e1hdal^9!s' +SECRET_KEY = os.getenv('SECRET_KEY') -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +if not SECRET_KEY: + raise ValueError("No SECRET_KEY environment variable set!") -ALLOWED_HOSTS = ['*'] +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = os.getenv('DEBUG', 'False').lower() == 'true' -CSRF_TRUSTED_ORIGINS = [ - 'https://*.defang.dev', - 'http://localhost:8000', -] +ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', 'localhost').split(',') # Application definition diff --git a/samples/django-postgres/compose.dev.yaml b/samples/django-postgres/compose.dev.yaml index 1964a7d3..d05bad43 100644 --- a/samples/django-postgres/compose.dev.yaml +++ b/samples/django-postgres/compose.dev.yaml @@ -17,6 +17,8 @@ services: - POSTGRES_USER=django - POSTGRES_DB=django - POSTGRES_PASSWORD=password + - SECRET_KEY=django-insecure-^0jq%7b(%aj$j@n0_$gk@#73&z#t%4o#klquddg1e1hdal^9!s + - ALLOWED_HOSTS=* volumes: - "./app:/code" command: python manage.py migrate diff --git a/samples/django-postgres/compose.yaml b/samples/django-postgres/compose.yaml index 09671430..1682f5b7 100644 --- a/samples/django-postgres/compose.yaml +++ b/samples/django-postgres/compose.yaml @@ -27,5 +27,7 @@ services: - POSTGRES_USER=django - POSTGRES_DB=django - POSTGRES_PASSWORD + - SECRET_KEY + - ALLOWED_HOSTS depends_on: - db