├── django_project ├── api │ ├── __init__.py │ ├── app1 │ │ ├── __init__.py │ │ ├── migrations │ │ │ └── __init__.py │ │ ├── management │ │ │ └── commands │ │ │ │ ├── __init__.py │ │ │ │ └── sample_management_command.py │ │ ├── serializers.py │ │ ├── admin.py │ │ ├── tests.py │ │ ├── models.py │ │ ├── urls.py │ │ ├── apps.py │ │ └── views.py │ ├── admin.py │ ├── apps.py │ └── urls.py ├── config │ ├── __init__.py │ ├── settings │ │ ├── stage.py │ │ ├── production.py │ │ ├── local.py │ │ └── base.py │ ├── asgi.py │ ├── wsgi.py │ └── urls.py ├── .env.sample ├── requirements.txt ├── entrypoint.sh ├── DockerFile ├── manage.py └── docker-compose.yml ├── LICENSE ├── .gitignore └── README.md /django_project/api/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /django_project/config/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /django_project/api/app1/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /django_project/api/app1/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /django_project/api/app1/management/commands/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /django_project/api/app1/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | -------------------------------------------------------------------------------- /django_project/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # TODO: Register your models here. 4 | -------------------------------------------------------------------------------- /django_project/api/app1/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # TODO: Register your models here. 4 | -------------------------------------------------------------------------------- /django_project/config/settings/stage.py: -------------------------------------------------------------------------------- 1 | from .base import * 2 | 3 | DEBUG = True 4 | ALLOWED_HOSTS = ["*"] 5 | -------------------------------------------------------------------------------- /django_project/api/app1/tests.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.urls import reverse 3 | from rest_framework.test import APITestCase 4 | -------------------------------------------------------------------------------- /django_project/api/app1/models.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | from django.db import models 3 | from django_extensions.db.models import TimeStampedModel 4 | 5 | -------------------------------------------------------------------------------- /django_project/.env.sample: -------------------------------------------------------------------------------- 1 | SECRET_KEY= 2 | POSTGRES_USER= 3 | POSTGRES_NAME= 4 | POSTGRES_PASSWORD= 5 | POSTGRES_HOST= 6 | POSTGRES_LOCAL_PORT= 7 | POSTGRES_PORT= 8 | DJANGO_PORT= -------------------------------------------------------------------------------- /django_project/api/app1/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import include, path 2 | from api.app1.views import home_view 3 | 4 | urlpatterns = [ 5 | path('home', home_view) 6 | ] 7 | -------------------------------------------------------------------------------- /django_project/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /django_project/api/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path, include 2 | from api.app1 import urls as app1_urls 3 | 4 | 5 | urlpatterns = [ 6 | path(r'app1/', include(app1_urls)) 7 | ] 8 | -------------------------------------------------------------------------------- /django_project/config/settings/production.py: -------------------------------------------------------------------------------- 1 | from .base import * 2 | 3 | # SECURITY WARNING: don't run with debug turned on in production! 4 | 5 | DEBUG = False 6 | ALLOWED_HOSTS = ["*"] 7 | -------------------------------------------------------------------------------- /django_project/api/app1/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api.app1' 7 | -------------------------------------------------------------------------------- /django_project/requirements.txt: -------------------------------------------------------------------------------- 1 | django >= 3.2, < 3.3 2 | django-environ==0.4.5 3 | django-extensions==3.1.3 4 | django-filter==2.4.0 5 | djangorestframework >= 3.12, < 3.13 6 | psycopg2-binary==2.8.6 -------------------------------------------------------------------------------- /django_project/config/settings/local.py: -------------------------------------------------------------------------------- 1 | from .base import * 2 | 3 | # SECURITY WARNING: don't run with debug turned on in production! 4 | 5 | DEBUG = True 6 | ALLOWED_HOSTS = ["*"] 7 | 8 | INTERNAL_IPS = [ 9 | '127.0.0.1', 10 | ] 11 | -------------------------------------------------------------------------------- /django_project/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Running Database Migrations" 4 | python manage.py makemigrations 5 | python manage.py migrate 6 | 7 | echo "Running app1 management commands" 8 | python manage.py sample_management_command 9 | 10 | exec "$@" -------------------------------------------------------------------------------- /django_project/api/app1/views.py: -------------------------------------------------------------------------------- 1 | from rest_framework.decorators import api_view 2 | from rest_framework.response import Response 3 | 4 | 5 | @api_view(['GET']) 6 | def home_view(request): 7 | """Returns successful response.""" 8 | return Response({'healthy': True}) 9 | -------------------------------------------------------------------------------- /django_project/DockerFile: -------------------------------------------------------------------------------- 1 | FROM python:3.8 2 | RUN apt-get update -qq && apt-get install -y -qq \ 3 | gdal-bin binutils libproj-dev libgdal-dev cmake &&\ 4 | apt-get clean all &&\ 5 | rm -rf /var/apt/lists/* &&\ 6 | rm -rf /var/cache/apt/* 7 | ENV PYTHONUNBUFFERED 1 8 | WORKDIR /django_project 9 | COPY . /django_project 10 | RUN pip install -r requirements.txt 11 | COPY entrypoint.sh /entrypoint.sh 12 | RUN chmod +x /entrypoint.sh -------------------------------------------------------------------------------- /django_project/api/app1/management/commands/sample_management_command.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | from django.core.management.base import BaseCommand 4 | 5 | logger = logging.getLogger('django_project_api') 6 | 7 | 8 | class Command(BaseCommand): 9 | help = 'Custom Management Command' 10 | 11 | def add_arguments(self, parser): 12 | pass 13 | 14 | def handle(self, *args, **options): 15 | logger.info("Custom Management Command") 16 | -------------------------------------------------------------------------------- /django_project/config/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for listings 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/3.2/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', 'config.settings.local') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /django_project/config/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for listings project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /django_project/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /django_project/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | web_app_db: 5 | restart: always 6 | container_name: web-app-db 7 | image: postgres:11 8 | environment: 9 | - POSTGRES_USER=${POSTGRES_USER} 10 | - POSTGRES_NAME=${POSTGRES_NAME} 11 | - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} 12 | ports: 13 | - "${POSTGRES_LOCAL_PORT}:${POSTGRES_PORT}" 14 | healthcheck: 15 | test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_NAME}"] 16 | interval: 5s 17 | timeout: 5s 18 | retries: 5 19 | 20 | django_backend: 21 | container_name: django-backend 22 | build: 23 | context: . 24 | dockerfile: DockerFile 25 | entrypoint: /entrypoint.sh 26 | command: bash -c "python manage.py runserver 0.0.0.0:${DJANGO_PORT}" 27 | ports: 28 | - "${DJANGO_PORT}:${DJANGO_PORT}" 29 | volumes: 30 | - .:/django_project 31 | depends_on: 32 | web_app_db: 33 | condition: service_healthy 34 | restart: on-failure 35 | -------------------------------------------------------------------------------- /django_project/config/urls.py: -------------------------------------------------------------------------------- 1 | """listings URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.2/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import include, path 18 | from django.conf import settings 19 | from django.conf.urls.static import static 20 | 21 | 22 | urlpatterns = [ 23 | path('admin/', admin.site.urls), 24 | path('api/', include('api.urls')), 25 | ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Palwisha Akhtar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | 162 | .idea 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django (DRF) Dockerized Project Template 2 | 3 | This repository serves as a template for creating a Django REST Framework (DRF) project that is containerized using Docker. It provides a structured starting point for developing Django-based web applications with a RESTful API, allowing for easier setup and deployment. 4 | 5 | ## Features 6 | 7 | - Django 3.x and Django REST Framework (DRF) pre-configured 8 | - PostgreSQL database integration 9 | - Docker and Docker Compose configuration for containerization 10 | - Separation of development and production settings 11 | - Automatic database setup and migrations using Django's `manage.py` commands 12 | - Ready-to-use Django project structure following best practices 13 | - Simple API endpoint for testing purposes 14 | - Customizable and extensible to fit your project's needs 15 | 16 | ## Prerequisites 17 | 18 | Before using this template, ensure that you have the following dependencies installed on your system: 19 | 20 | - Docker: [Install Docker](https://www.docker.com/get-started) 21 | - Docker Compose: [Install Docker Compose](https://docs.docker.com/compose/install/) 22 | 23 | ## Getting Started 24 | 25 | To start a new project using this template, follow these steps: 26 | 27 | 1. Clone this repository to your local machine: 28 | 29 | ```bash 30 | git clone https://github.com/Palwisha-18/django-drf-template.git 31 | ``` 32 | 33 | 2. Navigate into the `django_project` directory: 34 | 35 | ```bash 36 | cd django-drf-template 37 | cd django_project 38 | ``` 39 | 40 | 3. Set environment variables: 41 | 42 | ```bash 43 | cp .env.sample .env 44 | ``` 45 | 46 | Open the `.env` file and set the value of all environment variables. 47 | 48 | 4. Build and run the Docker containers: 49 | 50 | ```bash 51 | docker-compose up -d --build 52 | ``` 53 | 54 | This command will download the required Docker images, set up the containers, and start the development server. 55 | 56 | 5. Access the Django application: 57 | 58 | You can access the Django application running on `127.0.0.1:8000` in your web browser. 59 | 60 | 6. Test the API endpoint: 61 | 62 | A sample API endpoint is available at `127.0.0.1:8000/api/app1/home`. You can modify or extend it according to your project's requirements. 63 | 64 | ## Using this template 65 | - Rename `django_project` folder according to the name of your django project and replace its usage in the project. 66 | - Rename `app1` to represent the application name in your django project. Similarly, replace its usage in the project. 67 | - You can add more apps under api directory. For each app added, add app name to `INSTALLED_APPS` in `config/settings/base.py` and add app urls to `api/urls.py` 68 | 69 | ## Configuration 70 | 71 | - **Django Settings**: The project-specific Django settings are located in the `config/settings/` directory. You can modify these files to adjust the project's configuration based on your needs. 72 | 73 | - **Database**: By default, this template uses PostgreSQL as the database backend. You can update the database settings in the `docker-compose.yml` and `config/settings/base.py` 74 | 75 | - **Docker Configuration**: The Docker configuration files (`Dockerfile` and `docker-compose.yml`) are included in the root directory. If you need to make changes to the container setup or add additional services, you can modify these files accordingly. 76 | 77 | ## Development Workflow 78 | 79 | - During development, you can modify the Django code, and the changes will be automatically detected and applied. 80 | - To stop the development server and the associated containers, use the following command: 81 | 82 | ```bash 83 | docker-compose down 84 | ``` 85 | 86 | ## Deployment 87 | 88 | To deploy the project to a production environment, you can use Docker Compose or any other preferred deployment method. Ensure that you update the necessary configurations for a production environment, such as using a secure secret key, configuring HTTPS, and setting up any required production database or caching services. 89 | 90 | ## Contributing 91 | 92 | If you encounter any issues or have suggestions for improvements, feel free to open an issue or submit a pull request. 93 | 94 | ## License 95 | 96 | This project is licensed under the [MIT License](LICENSE). 97 | 98 | ## Acknowledgements 99 | 100 | This project template is inspired by the excellent Django and Docker tutorials and best practices available in the Django and Docker communities. 101 | -------------------------------------------------------------------------------- /django_project/config/settings/base.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for listings project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.2.4. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.2/ref/settings/ 11 | """ 12 | import environ 13 | import os 14 | from pathlib import Path 15 | 16 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 17 | BASE_DIR = Path(__file__).resolve().parent.parent 18 | 19 | 20 | # Quick-start development settings - unsuitable for production 21 | # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ 22 | 23 | env = environ.Env() 24 | 25 | # reading the EVN file 26 | environ.Env.read_env(BASE_DIR.__str__() + '/../.env') 27 | 28 | # Raises django's ImproperlyConfigured exception if SECRET_KEY not in os.environ 29 | SECRET_KEY = env('SECRET_KEY') 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'django_filters', 42 | 'api', 43 | 'api.app1', 44 | ] 45 | 46 | MIDDLEWARE = [ 47 | 'django.middleware.security.SecurityMiddleware', 48 | 'django.contrib.sessions.middleware.SessionMiddleware', 49 | 'django.middleware.common.CommonMiddleware', 50 | 'django.middleware.csrf.CsrfViewMiddleware', 51 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 52 | 'django.contrib.messages.middleware.MessageMiddleware', 53 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 54 | ] 55 | 56 | ROOT_URLCONF = 'config.urls' 57 | 58 | TEMPLATES = [ 59 | { 60 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 61 | 'DIRS': [], 62 | 'APP_DIRS': True, 63 | 'OPTIONS': { 64 | 'context_processors': [ 65 | 'django.template.context_processors.debug', 66 | 'django.template.context_processors.request', 67 | 'django.contrib.auth.context_processors.auth', 68 | 'django.contrib.messages.context_processors.messages', 69 | ], 70 | }, 71 | }, 72 | ] 73 | 74 | WSGI_APPLICATION = 'config.wsgi.application' 75 | 76 | 77 | # Database 78 | # https://docs.djangoproject.com/en/3.2/ref/settings/#databases 79 | POSTGRES_USER = env('POSTGRES_USER') 80 | POSTGRES_NAME = env('POSTGRES_NAME') 81 | POSTGRES_PASSWORD = env('POSTGRES_PASSWORD') 82 | POSTGRES_HOST = env('POSTGRES_HOST') 83 | POSTGRES_PORT = env('POSTGRES_PORT') 84 | DATABASES = { 85 | 'default': { 86 | 'ENGINE': 'django.db.backends.postgresql', 87 | 'NAME': POSTGRES_NAME, 88 | 'USER': POSTGRES_USER, 89 | 'PASSWORD': POSTGRES_PASSWORD, 90 | 'HOST': POSTGRES_HOST, 91 | 'PORT': POSTGRES_PORT, 92 | }, 93 | } 94 | 95 | 96 | # Password validation 97 | # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators 98 | 99 | AUTH_PASSWORD_VALIDATORS = [ 100 | { 101 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 102 | }, 103 | { 104 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 105 | }, 106 | { 107 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 108 | }, 109 | { 110 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 111 | }, 112 | ] 113 | 114 | 115 | # Internationalization 116 | # https://docs.djangoproject.com/en/3.2/topics/i18n/ 117 | 118 | LANGUAGE_CODE = 'en-us' 119 | 120 | TIME_ZONE = 'UTC' 121 | 122 | USE_I18N = True 123 | 124 | USE_L10N = True 125 | 126 | USE_TZ = True 127 | 128 | 129 | # Static files (CSS, JavaScript, Images) 130 | # https://docs.djangoproject.com/en/3.2/howto/static-files/ 131 | 132 | STATIC_URL = '/static/' 133 | STATIC_ROOT = os.path.join(BASE_DIR, "static") 134 | 135 | # Default primary key field type 136 | # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field 137 | 138 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 139 | 140 | REST_FRAMEWORK = { 141 | # Use Django's standard `django.contrib.auth` permissions, 142 | # or allow read-only access for unauthenticated users. 143 | 'DEFAULT_PERMISSION_CLASSES': [ 144 | 'rest_framework.permissions.AllowAny' 145 | ] 146 | } 147 | 148 | # Logging configuration 149 | 150 | LOGGING = { 151 | 'version': 1, 152 | 'disable_existing_loggers': False, 153 | 'formatters': { 154 | 'verbose': { 155 | 'format': '{asctime} {levelname} {filename} {funcName} {lineno} {message}', 156 | 'style': '{', 157 | }, 158 | 'simple': { 159 | 'format': '{asctime} {levelname} - {message}', 160 | 'style': '{', 161 | }, 162 | }, 163 | 'filters': { 164 | 'require_debug_false': { 165 | '()': 'django.utils.log.RequireDebugFalse', 166 | }, 167 | 'require_debug_true': { 168 | '()': 'django.utils.log.RequireDebugTrue', 169 | }, 170 | }, 171 | 'handlers': { 172 | 'console': { 173 | 'level': 'DEBUG', 174 | 'class': 'logging.StreamHandler', 175 | 'filters': ['require_debug_true'], 176 | 'formatter': 'simple', 177 | }, 178 | 'log_file': { 179 | 'level': 'DEBUG', 180 | 'class': 'logging.handlers.RotatingFileHandler', 181 | 'filename': 'api.log', 182 | 'maxBytes': 1024 * 1024 * 5, # 5 mb 183 | 'backupCount': 5, 184 | 'formatter': 'verbose', 185 | }, 186 | 'error_file': { 187 | 'level': 'ERROR', 188 | 'class': 'logging.handlers.RotatingFileHandler', 189 | 'filename': 'api_error.log', 190 | 'maxBytes': 1024 * 1024 * 5, # 5 mb 191 | 'backupCount': 5, 192 | 'formatter': 'verbose', 193 | }, 194 | 'mail_admins': { 195 | 'level': 'ERROR', 196 | 'class': 'django.utils.log.AdminEmailHandler', 197 | 'filters': ['require_debug_false'], 198 | 'formatter': 'verbose', 199 | }, 200 | }, 201 | 'loggers': { 202 | 'django_project_api': { 203 | 'handlers': ['console', 'log_file', 'error_file', 'mail_admins'], 204 | 'level': 'DEBUG', 205 | }, 206 | 'django.request': { 207 | 'handlers': ['mail_admins', 'error_file'], 208 | 'level': 'ERROR', 209 | 'propagate': False, 210 | }, 211 | }, 212 | } 213 | --------------------------------------------------------------------------------