Skip to content

Commit

Permalink
Add Django files
Browse files Browse the repository at this point in the history
  • Loading branch information
ddahan committed Sep 19, 2023
1 parent d789ce4 commit acb14bf
Show file tree
Hide file tree
Showing 11 changed files with 734 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/.env_db
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POSTGRES_DB=mydjangodb
POSTGRES_DB=mydb
POSTGRES_USER=myuser
POSTGRES_PASSWORD=mypassword
4 changes: 2 additions & 2 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
# -- Django container --
app:
# vars are injected at runtime, so no need to specify env_var value here.
image: 'postgres/15-bullseye'
image: 'python:3.11.5-bullseye'

volumes:
- ..:/workspace:delegated
Expand All @@ -18,7 +18,7 @@ services:
# -- PostgreSQL container --
db:
env_file: .env_db # var created at build-time, so we need an env_file
image: 'postgres/15-bullseye'
image: 'postgres:15-bullseye'
restart: unless-stopped

volumes:
Expand Down
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DJ_ENV_NAME="local"
DJ_SECRET_KEY="some-key-to-change-to-be-safe"
DJ_DEBUG="true"
DJ_DATABASE_URL="myuser:mypassword@localhost:5432/mydb"
Empty file added dj_config/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions dj_config/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dj_config.settings')

application = get_asgi_application()
107 changes: 107 additions & 0 deletions dj_config/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import os
import sys
from pathlib import Path
import environ
import dj_database_url

##########################################################################################
# Path
##########################################################################################

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

APP_ROOT = "dj_apps"
APPS_FOLDERS = [
APP_ROOT,
# TODO: add f"{APP_ROOT}/<my_app_name>" for each new app
]
for folder in APPS_FOLDERS:
sys.path.insert(0, os.path.join(BASE_DIR, folder))


##########################################################################################
# Environment
# https://django-environ.readthedocs.io/en/latest/quickstart.html
##########################################################################################

env = environ.Env()

# Take environment variables from .env file (if it exists)
# That's why it is important to NOT version .env file
# (otherwise prod environment will get local env file values!)
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

ENV_NAME = env("DJ_ENV_NAME")

##########################################################################################
# Security
##########################################################################################

SECRET_KEY = env("DJ_SECRET_KEY")
DEBUG = env("DJ_DEBUG", cast=bool)
CORS_ORIGIN_ALLOW_ALL = True
ALLOWED_HOSTS = ["*"] # To edit according your hosting platform


##########################################################################################
# Apps definition
##########################################################################################

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"corsheaders",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware", # to handle CORS with right headers
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "dj_config.urls"
WSGI_APPLICATION = "dj_config.wsgi.application"


##########################################################################################
# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
##########################################################################################


DATABASES = {"default": dj_database_url.config(env="DJ_DATABASE_URL", conn_max_age=600)}

##########################################################################################
# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
##########################################################################################

LANGUAGE_CODE = "fr"
TIME_ZONE = "Europe/Paris"
USE_I18N = True
USE_L10N = True
USE_TZ = True

##########################################################################################
# Static files
# https://docs.djangoproject.com/en/dev/howto/static-files/
##########################################################################################

STATIC_URL = "/static/"
STATIC_ROOT = BASE_DIR / "staticfiles"

##########################################################################################
# Misc
##########################################################################################

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
6 changes: 6 additions & 0 deletions dj_config/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('dj_admin/', admin.site.urls),
]
7 changes: 7 additions & 0 deletions dj_config/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dj_config.settings')

application = get_wsgi_application()
22 changes: 22 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toto.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Loading

0 comments on commit acb14bf

Please sign in to comment.