Skip to content

Commit 412d506

Browse files
Merge pull request #266 from DefangLabs/linda-django
django changes
2 parents a6612f0 + 2e4078c commit 412d506

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

README.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,55 @@
44

55
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!
66

7-
## NOTE
7+
> [!NOTE]
8+
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.
89

9-
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 Rail'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.
10+
## Prerequisites
1011

11-
## Essential Setup Files
12+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
13+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
14+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
1215

13-
1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) to describe the basic image of your applications.
14-
2. A [docker-compose file](https://docs.defang.io/docs/concepts/compose) to define and run multi-container Docker applications.
15-
3. A [.dockerignore](https://docs.docker.com/build/building/context/#dockerignore-files) file to comply with the size limit (10MB).
16+
## Development
1617

17-
## Prerequisite
18+
To run the application locally, you can use the following command:
1819

19-
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
20-
2. If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc), make sure you have properly [authenticated your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
21-
Plus, make sure that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
20+
```bash
21+
docker compose up --build
22+
```
23+
24+
## Configuration
25+
26+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):
27+
28+
> 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.
29+
30+
### `POSTGRES_PASSWORD`
31+
```bash
32+
defang config set POSTGRES_PASSWORD
33+
```
34+
35+
## Deployment
36+
37+
> [!NOTE]
38+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
39+
40+
### Defang Playground
41+
42+
Deploy your application to the Defang Playground by opening up your terminal and typing:
43+
```bash
44+
defang compose up
45+
```
46+
47+
### BYOC (AWS)
2248

23-
## A Step-by-Step Guide for Deployment
49+
If you want to deploy to your own cloud account, you can use Defang BYOC:
2450

25-
1. Open the terminal and type `defang login`
26-
2. Type `defang compose up` in the CLI
27-
3. Now your application will be launched
51+
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
52+
2. Run in a terminal that has access to your AWS environment variables:
53+
```bash
54+
defang --provider=aws compose up
55+
```
2856

2957
---
3058

app/crm_platform/settings.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
'django.contrib.auth.middleware.AuthenticationMiddleware',
5454
'django.contrib.messages.middleware.MessageMiddleware',
5555
'django.middleware.clickjacking.XFrameOptionsMiddleware',
56-
'whitenoise.middleware.WhiteNoiseMiddleware',
56+
'whitenoise.middleware.WhiteNoiseMiddleware',
5757
]
5858

5959
ROOT_URLCONF = 'crm_platform.urls'
@@ -83,13 +83,13 @@
8383
DATABASES = {
8484
"default": {
8585
"ENGINE": "django.db.backends.postgresql",
86-
"HOST": os.environ.get('DB_HOST', 'db'),
87-
# We put the host in an environment variable because Defang
86+
"HOST": os.environ.get('DB_HOST', 'db'),
87+
# We put the host in an environment variable because Defang
8888
# detects hostnames that match service names and makes sure they
8989
# are properly configured to communicate when deployed
90-
"NAME": 'mydatabase',
91-
"USER": 'myuser',
92-
"PASSWORD": 'mypassword',
90+
"NAME": os.environ.get("POSTGRES_DB"),
91+
"USER": os.environ.get("POSTGRES_USER"),
92+
"PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
9393
}
9494
}
9595

compose.yaml

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
services:
22
db:
33
restart: unless-stopped
4-
image: postgres:14
4+
image: postgres:16
55
volumes: ["./tmp/postgres:/var/lib/postgresql/data/"]
66
environment:
77
- POSTGRES_DB=mydatabase
88
- POSTGRES_USER=myuser
9-
- POSTGRES_PASSWORD=mypassword
9+
- POSTGRES_PASSWORD
1010
ports:
1111
- mode: host
1212
target: 5432
13-
healthcheck:
14-
test:
15-
[
16-
"CMD-SHELL",
17-
"PGUSER=myuser PGPASSWORD=mypassword pg_isready -q -d mydatabase",
18-
]
19-
interval: 1m30s
20-
timeout: 30s
21-
retries: 5
2213
#deploy:
2314
# resources:
2415
# reservations:
@@ -35,10 +26,13 @@ services:
3526
environment:
3627
- DB_HOST=db
3728
- DEBUG=False
38-
deploy:
39-
resources:
40-
reservations:
41-
memory: 2GB
29+
- POSTGRES_USER=myuser
30+
- POSTGRES_DB=mydatabase
31+
- POSTGRES_PASSWORD
32+
# deploy:
33+
# resources:
34+
# reservations:
35+
# memory: 2GB
4236
# volumes: [".:/code"]
4337
depends_on:
4438
- db

0 commit comments

Comments
 (0)