Skip to content

Commit 4f1545b

Browse files
authored
Merge pull request #307 from DefangLabs/more-django-fixes
More fixes for Django Postgres sample
0 parents  commit 4f1545b

34 files changed

+1138
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM mcr.microsoft.com/devcontainers/python:3.12-bookworm

.devcontainer/devcontainer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": ".."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
9+
"ghcr.io/devcontainers/features/aws-cli:1": {}
10+
}
11+
}

.github/workflows/deploy.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]
22+
with:
23+
config-env-vars: POSTGRES_PASSWORD SECRET_KEY ALLOWED_HOSTS
24+
env:
25+
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
26+
SECRET_KEY: ${{ secrets.SECRET_KEY }}
27+
ALLOWED_HOSTS: ${{ secrets.ALLOWED_HOSTS }}

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Django & Postgres
2+
3+
[![1-click-deploy](https://defang.io/deploy-with-defang.png)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-django-postgres-template%26template_owner%3DDefangSamples)
4+
5+
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!
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
14+
15+
To run the application locally, you can use the following command:
16+
17+
```bash
18+
docker compose up --build
19+
```
20+
21+
## Configuration
22+
23+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):
24+
25+
> 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.
26+
27+
### `POSTGRES_PASSWORD`
28+
29+
The password for the PostgreSQL database.
30+
31+
```bash
32+
defang config set POSTGRES_PASSWORD
33+
```
34+
35+
### `SECRET_KEY`
36+
37+
The secret key is used to secure the Django application.
38+
39+
```bash
40+
defang config set SECRET_KEY
41+
```
42+
43+
### `ALLOWED_HOSTS`
44+
45+
The allowed hosts for the Django application. (i.e. the domain your app runs on)
46+
47+
```bash
48+
defang config set ALLOWED_HOSTS
49+
```
50+
51+
## Deployment
52+
53+
> [!NOTE]
54+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
55+
56+
### Defang Playground
57+
58+
Deploy your application to the Defang Playground by opening up your terminal and typing:
59+
```bash
60+
defang compose up
61+
```
62+
63+
### BYOC (AWS)
64+
65+
If you want to deploy to your own cloud account, you can use Defang BYOC:
66+
67+
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`.
68+
2. Run in a terminal that has access to your AWS environment variables:
69+
```bash
70+
defang --provider=aws compose up
71+
```
72+
73+
---
74+
75+
Title: Django & PostgreSQL
76+
77+
Short Description: A customer relationship management list project developed using the Python Django framework, offering a starting point to help you quickly build your customer management system.
78+
79+
Tags: Django, PostgreSQL, Python, SQL
80+
81+
Languages: python

app/.dockerignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Ignore version control directories
2+
.git
3+
.gitignore
4+
5+
# Ignore all bytecode
6+
*.pyc
7+
__pycache__/
8+
9+
# Ignore various log and data directories that are created while running locally
10+
logs/
11+
data/
12+
13+
# Ignore virtual environment directories
14+
virt/
15+
16+
# Ignore test, documentation, and CI configuration
17+
tests/
18+
docs/
19+
*.md
20+
.circleci/
21+
.travis.yml
22+
.gitlab-ci.yml
23+
24+
# Ignore PostgreSQL specific files
25+
*.sql
26+
*.dump
27+
28+
# Ignore other unnecessary files
29+
*.log
30+
*.tar.gz
31+
*.zip
32+
tmp/

app/.gitignore

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
tmp
2+
3+
# Django #
4+
*.log
5+
*.pot
6+
*.pyc
7+
__pycache__
8+
db.sqlite3
9+
media
10+
11+
# Backup files #
12+
*.bak
13+
14+
# If using celery-beat with pickle as serializer #
15+
celerybeat-schedule.bak
16+
17+
# PyCharm #
18+
.idea/
19+
20+
# VS Code #
21+
.vscode/
22+
23+
# Python #
24+
*.py[cod]
25+
*$py.class
26+
27+
# Distribution / packaging #
28+
.Python
29+
env/
30+
build/
31+
develop-eggs/
32+
dist/
33+
downloads/
34+
eggs/
35+
.eggs/
36+
lib/
37+
lib64/
38+
parts/
39+
sdist/
40+
var/
41+
wheels/
42+
*.egg-info/
43+
.installed.cfg
44+
*.egg
45+
46+
# Installer logs #
47+
pip-log.txt
48+
pip-delete-this-directory.txt
49+
50+
# Unit test / coverage reports #
51+
htmlcov/
52+
.tox/
53+
.coverage
54+
.coverage.*
55+
.cache
56+
nosetests.xml
57+
coverage.xml
58+
*.cover
59+
.hypothesis/
60+
61+
# Translations #
62+
*.mo
63+
*.pot
64+
65+
# Django stuff #
66+
*.log
67+
local_settings.py
68+
db.sqlite3
69+
db.sqlite3-journal
70+
71+
# Flask stuff #
72+
instance/
73+
.webassets-cache
74+
75+
# Scrapy stuff #
76+
.scrapy
77+
78+
# Sphinx documentation #
79+
docs/_build/
80+
81+
# PyBuilder #
82+
target/
83+
84+
# Jupyter Notebook #
85+
.ipynb_checkpoints
86+
87+
# IPython #
88+
profile_default/
89+
ipython_config.py
90+
91+
# pyenv #
92+
.python-version
93+
94+
# celery beat schedule file #
95+
celerybeat-schedule
96+
97+
# SageMath parsed files #
98+
*.sage.py
99+
100+
# dotenv #
101+
.env
102+
103+
# virtualenv #
104+
venv/
105+
ENV/
106+
107+
# Spyder project settings #
108+
.spyderproject
109+
.spyproject
110+
111+
# Rope project settings #
112+
.ropeproject
113+
114+
# mkdocs documentation #
115+
/site
116+
117+
# mypy #
118+
.mypy_cache/
119+
.dmypy.json
120+
dmypy.json
121+
122+
# Pyre type checker #
123+
.pyre/
124+
125+
# pytype static type analyzer #
126+
.pytype/
127+
128+
# Cython debug symbols #
129+
cython_debug/
130+
131+
# Profiling data #
132+
.prof
133+
134+
# Ignore the static files
135+
staticfiles/

app/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.11-slim
3+
4+
RUN apt-get update -qq \
5+
&& apt-get install -y curl \
6+
&& apt-get clean \
7+
&& rm -rf /var/lib/apt/lists/*
8+
9+
# Set environment variables
10+
ENV PYTHONDONTWRITEBYTECODE 1
11+
ENV PYTHONUNBUFFERED 1
12+
13+
# Set work directory
14+
WORKDIR /code
15+
16+
# Install dependencies
17+
COPY ./requirements.txt .
18+
RUN pip install -r requirements.txt
19+
20+
# Copy project
21+
COPY . /code/
22+
23+
# Collect static files
24+
RUN python manage.py collectstatic --noinput
25+
26+
# Start server
27+
CMD python manage.py migrate && python manage.py createsuperauto && gunicorn app.wsgi:application --bind 0.0.0.0:8000

app/app/__init__.py

Whitespace-only changes.

app/app/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for app project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
15+
16+
application = get_asgi_application()

0 commit comments

Comments
 (0)