This repository is one exemple of build project with Django Framework
https://pyup.io/repos/github/brjatoba92/DjangoCourseJatoba/shield.svg https://pyup.io/repos/github/brjatoba92/DjangoCourseJatoba/python-3-shield.svg
Install libs:
- Django:
pip install django
- python-decouple
pip install python-decouple
- dj-database-url
pip install dj-database-url
- psycopg2 lib
pip install psycopg2-binary
- gunicorn:
pip install gunicorn
- Whitenoise:
pip install whitenoise
- Generate requirements.txt
pip freeze > requirements.txt
Start Project
- Create manage.py file:
django-admin startproject fly_django_project .
- Run server project with manage.py:
python manage.py --help
python manage.py runserver
Fly.io (Changindo in place heroku for deploy api):
- Install flyctl
curl -L https://fly.io/install.sh | sh
- Manually add the directory to your $HOME/.bash_profile (or similar)
export FLYCTL_INSTALL="/home/brunojat/.fly"
export PATH="$FLYCTL_INSTALL/bin:$PATH"
- Save the .bashrc
source .bashrc
- Create .bash_aliases file with alias fly
alias fly='/home/brunojat/.fly/bin/flyctl'
source .bash_aliases
- Fly auth login
fly auth login
- Run fly launch
fly launch
fly-django-jatoba
choose the region (rio de janeiro)
Would you like to set up a Postgresql database now? y
Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
DATABASE_URL=postgres://fly_django_jatoba:sYh8whbxjkARML0@fly-django-jatoba-db.flycast:5432/fly_django_jatoba?sslmode=disable
Would you like to set up an Upstash Redis database now? N
- Fly deploy
fly deploy
- Fly secret list show the secrets keys:
fly secret list
fly secrets set ALLOWED_HOSTS=fly-django-jatoba.fly.dev,
- Edit fly.toml and add in final this line:
[deploy]
release_command = "python manage.py migrate --noinput"
[[statics]]
guest_path = "/code/public"
url_prefix = "/static/"
- Create superuser:
fly ssh console -t
user
email
password
Docker:
- Docker install:
https://github.com/codeedu/wsl2-docker-quickstart
- Docker commands:
docker --help
docker --version
sudo service docker start
sudo service docker status
sudo service docker restart
docker build -t fly-deploy .
docker ps
docker stop 381a4e2e55f0
docker stop fly_django_project
docker run --rm -it -p 8000:8000 -e SECRET_KEY=secret -e ALLOWED_HOSTS=localhost, fly-deploy .
sudo chmod +x start.sh
docker run --name fly_django_project -it -p 8000:8000 -e SECRET_KEY=secret -e ALLOWED_HOSTS=localhost, fly-deploy python manage.py migrate --noinput + python manage.py collectstatic --noinput
docker run --rm -it -p 8000:8000 -e SECRET_KEY=secret -e ALLOWED_HOSTS=localhost, fly-deploy python manage.py migrate
Documments:
- Settings.py
from decouple import config, Csv
from dj_database_url import parse as db_url
...
DEBUG = config('DEBUG', cast=bool, default=False)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv(), default='localhost,')
### ['fly-django-jatoba.fly.dev']
SECRET_KEY = config('SECRET_KEY')
...
INSTALLED_APPS = [
...
'fly_django_project',
]
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...
]
DATABASES = {
'default': config(
'DATABASE_URL',
default='sqlite: ///' + str(BASE_DIR / 'db.sqlite3'),
cast=db_url
)
}
...
STATIC_ROOT = BASE_DIR / 'public'
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
...
CSRF_TRUSTED_ORIGINS = ['https://fly-django-jatoba.fly.dev/']
- Dockerfile
# No used with whitenoise
# RUN python manage.py collectstatic --noinput
# replace demo.wsgi with <project_name>.wsgi
CMD ["gunicorn", "--bind", ":8000", "--workers", "2", "fly_django_project.wsgi"]
- .env file:
- Create .env and storage the variabel SECRET_KEY available in settings.py. Add DEBUG=True and ALLOWED_HOSTS=localhost, this file
-
Create .env.example and copy all the contents of the .env file changing the SECRET_KEY=secret
-
Start.sh (no used)
-
Create .dockerignore and add .env for not send the .env file
- Model:
https://github.com/GoogleCloudPlatform/getting-started-python/blob/main/optional-kubernetes-engine/.dockerignore
Run Server and Migrate Commands:
- Migrate:
python manage.py migrate
- Run server:
python manage.py runserver
http://localhost:8000/
Whitenoise
- settings.py:
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...
]
...
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
Github Actions
- Create django.yml
https://github.com/brjatoba92/DjangoCourseJatoba/actions/new
- Test