Skip to content

Commit 337a19f

Browse files
committed
Updated diploi.yaml to generate secret key during deployment launch
Modified common tasks using uv Added CSRF origin on settings.py
1 parent 6bbaef2 commit 337a19f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ To get all static files, the image runs the command:
5959
Lastly, the image will start the production server with the command:
6060
`uv run gunicorn djangoapp.wsgi:application --bind 0.0.0.0:8000 --workers 3 --log-level info`
6161

62+
### Common tasks (using uv)
63+
64+
- Create a superuser
65+
`uv run python manage.py createsuperuser`
66+
67+
- Run migrations
68+
`uv run python manage.py migrate`
69+
70+
- Switch the database to Postgres instead of SQLite
71+
1. Install the Postgres driver (already present in `pyproject.toml`, but if missing run `uv add "psycopg2-binary"`).
72+
2. In `djangoapp/settings.py`, set `DATABASES["default"]` to read from `DATABASE_URL` (e.g. using `env.dj_db_url` if `environs[django]` is installed). A typical value:
73+
`postgres://postgres:postgres@postgres.postgres:5432/app`
74+
3. Export `DATABASE_URL` in your environment (or add to `.env`), then run `uv run python manage.py migrate`.
75+
76+
- Adjust CSRF trusted origins
77+
In `djangoapp/settings.py`, the list comes from `CSRF_TRUSTED_ORIGINS` env var. Add hosts as a comma-separated list, e.g.:
78+
`export CSRF_TRUSTED_ORIGINS="https://example.com,https://admin.example.com"`
79+
After updating, restart the app so Django picks up the new origins.
80+
6281
## Links
6382

6483
- [Adding Django to a project](https://docs.diploi.com/building/components/django)

diploi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ connectionStrings:
1717
value: http://app.${DIPLOI_NAMESPACE}:8000
1818
description: This address is for requests from within the deployment and is inaccessible externally.
1919

20+
parameterGroups:
21+
- name: Secret Key
22+
identifier: secret_key
23+
description: Django Secret Key
24+
parameters:
25+
- name: Secret Key
26+
identifier: SECRET_KEY
27+
defaultValue: '${generate-random-password:50}'
28+
type: secret
29+
2030
images:
2131
- identifier: app
2232
prebuildImage: ghcr.io/diploi/component-django:[tag]

djangoapp/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
# THIS KEY IS UNSAFE AND ONLY MEANT TO BE USED WHEN TESTING, generate your own SECRET_KEY. You can use https://djecrety.ir/ to generate a new SECRET_KEY
3030
SECRET_KEY = '8um(xc_7mmw3frkj+yvz__ak$$5$o1_vl#esv-3ue0-%&n@gui'
3131

32+
if 'SECRET_KEY' in os.environ:
33+
SECRET_KEY = os.environ['SECRET_KEY']
34+
3235
# SECURITY WARNING: don't run with debug turned on in production!
3336
DEBUG = True
3437
# if 'STAGE' != 'development' in os.environ:
@@ -41,6 +44,9 @@
4144
if 'APP_PUBLIC_URL' in os.environ:
4245
ALLOWED_HOSTS.append(os.environ['APP_PUBLIC_URL'])
4346

47+
CSRF_TRUSTED_ORIGINS = env.list("CSRF_TRUSTED_ORIGINS", default=[])
48+
if "APP_PUBLIC_URL" in os.environ:
49+
CSRF_TRUSTED_ORIGINS.append(f"https://{os.environ['APP_PUBLIC_URL']}")
4450

4551
# Application definition
4652

0 commit comments

Comments
 (0)