├── .github └── workflows │ ├── django.yml │ └── linter_and_formatter.yml ├── .gitignore ├── .pre-commit-config.yaml ├── Dockerfile ├── MIT-LICENSE.txt ├── README.md ├── docker-compose.yml ├── entrypoint.sh ├── manage.py ├── requirements.txt ├── ruff.toml ├── sampleapp ├── __init__.py ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ └── __init__.py ├── models.py ├── serializers.py ├── tests.py ├── urls.py └── views.py ├── sampleproject ├── __init__.py ├── asgi.py ├── urls.py └── wsgi.py └── settings ├── __init__.py ├── base.py ├── development.py └── production.py /.github/workflows/django.yml: -------------------------------------------------------------------------------- 1 | name: Django CI 2 | 3 | on: 4 | push: 5 | branches: ["master"] 6 | pull_request: 7 | branches: ["master"] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | max-parallel: 4 14 | matrix: 15 | python-version: ["3.11", "3.10"] 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | - name: Set up Python ${{ matrix.python-version }} 20 | uses: actions/setup-python@v3 21 | with: 22 | python-version: ${{ matrix.python-version }} 23 | - name: Install Dependencies 24 | run: | 25 | python -m pip install --upgrade pip 26 | pip install -r requirements.txt 27 | - name: Run Tests 28 | env: 29 | SECRET_KEY: ${{ secrets.SECRET_KEY }} 30 | DATABASE_NAME: ${{ secrets.DATABASE_NAME }} 31 | DATABASE_USER: ${{ secrets.DATABASE_USER }} 32 | DATABASE_PASS: ${{ secrets.DATABASE_PASS }} 33 | DATABASE_HOST: ${{ secrets.DATABASE_HOST }} 34 | DATABASE_PORT: ${{ secrets.DATABASE_PORT }} 35 | run: | 36 | python manage.py test 37 | -------------------------------------------------------------------------------- /.github/workflows/linter_and_formatter.yml: -------------------------------------------------------------------------------- 1 | name: Python Linting and Formatting 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | lint: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v2 11 | 12 | - name: Set up Python 13 | uses: actions/setup-python@v2 14 | with: 15 | python-version: "3.x" 16 | 17 | - name: Install dependencies 18 | run: | 19 | python -m pip install --upgrade pip 20 | pip install -r requirements.txt 21 | 22 | - name: Lint with Ruff 23 | run: ruff . 24 | 25 | - name: Format with Black 26 | run: black . 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | django_env 2 | __pycache__ 3 | db.sqlite3 4 | 5 | .DS_STORE 6 | node_modules 7 | scripts/flow/*/.flowconfig 8 | .flowconfig 9 | sampleproject/settings.py 10 | *~ 11 | *.pyc 12 | .grunt 13 | _SpecRunner.html 14 | __benchmarks__ 15 | build/ 16 | remote-repo/ 17 | coverage/ 18 | .module-cache 19 | fixtures/dom/public/react-dom.js 20 | fixtures/dom/public/react.js 21 | test/the-files-to-test.generated.js 22 | *.log* 23 | chrome-user-data 24 | *.sublime-project 25 | *.sublime-workspace 26 | .idea 27 | *.iml 28 | .vscode 29 | *.swp 30 | *.swo 31 | 32 | packages/react-devtools-core/dist 33 | packages/react-devtools-extensions/chrome/build 34 | packages/react-devtools-extensions/chrome/*.crx 35 | packages/react-devtools-extensions/chrome/*.pem 36 | packages/react-devtools-extensions/firefox/build 37 | packages/react-devtools-extensions/firefox/*.xpi 38 | packages/react-devtools-extensions/firefox/*.pem 39 | packages/react-devtools-extensions/shared/build 40 | packages/react-devtools-extensions/.tempUserDataDir 41 | packages/react-devtools-inline/dist 42 | packages/react-devtools-shell/dist 43 | packages/react-devtools-timeline/dist 44 | 45 | !**/glob-import/dir/node_modules 46 | .DS_Store 47 | .idea 48 | *.cpuprofile 49 | *.local 50 | *.log 51 | /.vscode/ 52 | /docs/.vitepress/cache 53 | /packages/vite/LICENSE 54 | dist 55 | dist-ssr 56 | explorations 57 | node_modules 58 | playground-temp 59 | temp 60 | TODOs.md 61 | .eslintcache 62 | 63 | # Created by https://www.gitignore.io 64 | 65 | ### OSX ### 66 | .DS_Store 67 | .AppleDouble 68 | .LSOverride 69 | 70 | # Icon must end with two \r 71 | Icon 72 | 73 | 74 | # Thumbnails 75 | ._* 76 | 77 | # Files that might appear on external disk 78 | .Spotlight-V100 79 | .Trashes 80 | 81 | # Directories potentially created on remote AFP share 82 | .AppleDB 83 | .AppleDesktop 84 | Network Trash Folder 85 | Temporary Items 86 | .apdisk 87 | 88 | 89 | ### Python ### 90 | # Byte-compiled / optimized / DLL files 91 | __pycache__/ 92 | *.py[cod] 93 | 94 | # C extensions 95 | *.so 96 | 97 | # Distribution / packaging 98 | .Python 99 | env/ 100 | build/ 101 | develop-eggs/ 102 | dist/ 103 | downloads/ 104 | eggs/ 105 | lib/ 106 | lib64/ 107 | parts/ 108 | sdist/ 109 | var/ 110 | *.egg-info/ 111 | .installed.cfg 112 | *.egg 113 | 114 | # PyInstaller 115 | # Usually these files are written by a python script from a template 116 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 117 | *.manifest 118 | *.spec 119 | 120 | # Installer logs 121 | pip-log.txt 122 | pip-delete-this-directory.txt 123 | 124 | # Unit test / coverage reports 125 | htmlcov/ 126 | .tox/ 127 | .coverage 128 | .cache 129 | nosetests.xml 130 | coverage.xml 131 | 132 | # Translations 133 | *.mo 134 | *.pot 135 | 136 | # Sphinx documentation 137 | docs/_build/ 138 | 139 | # PyBuilder 140 | target/ 141 | 142 | 143 | ### Django ### 144 | *.log 145 | *.pot 146 | *.pyc 147 | __pycache__/ 148 | local_settings.py 149 | 150 | .env* 151 | db.sqlite3 152 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: v2.3.0 4 | hooks: 5 | - id: check-yaml 6 | - id: end-of-file-fixer 7 | - id: trailing-whitespace 8 | - repo: https://github.com/psf/black 9 | rev: 22.10.0 10 | hooks: 11 | - id: black 12 | - repo: https://github.com/charliermarsh/ruff-pre-commit 13 | # Ruff version. 14 | rev: "v0.0.257" 15 | hooks: 16 | - id: ruff 17 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.9.6-alpine 2 | 3 | WORKDIR /app 4 | 5 | # set environment variables 6 | ENV PYTHONDONTWRITEBYTECODE 1 7 | ENV PYTHONUNBUFFERED 1 8 | 9 | RUN apk update \ 10 | && apk add postgresql-dev gcc python3-dev musl-dev 11 | 12 | # install dependencies 13 | RUN pip install --upgrade pip 14 | COPY ./requirements.txt . 15 | RUN pip install -r requirements.txt 16 | 17 | # copy entrypoint.sh 18 | COPY ./entrypoint.sh . 19 | RUN sed -i 's/\r$//g' /app/entrypoint.sh 20 | RUN chmod +x /app/entrypoint.sh 21 | 22 | # copy project 23 | COPY . . 24 | 25 | # run entrypoint.sh 26 | ENTRYPOINT ["/app/entrypoint.sh"] 27 | -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2022 Scott Chacon and others 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django Best Practices 2 | 3 | This repository provides a collection of best practices for developing Django web applications. These practices are based on industry standards and can help you write more robust, maintainable, and scalable Django code. 4 | 5 | ## Table of Contents 6 | 7 | - [Getting Started](#getting-started) 8 | - [Project Structure](#project-structure) 9 | - [Environment Configuration](#environment-configuration) 10 | - [PostgreSQL Database Connection](#postgresql-database-connection) 11 | - [Security](#security) 12 | - [Testing](#testing) 13 | - [Deployment](#deployment) 14 | - [Contributing](#contributing) 15 | - [License](#license) 16 | 17 | ## Getting Started 18 | 19 | First clone the repository from Github and switch to the new directory: 20 | 21 | $ git clone https://github.com/pratyzsh/django-best-practices 22 | $ cd django-best-practices 23 | 24 | Activate the virtualenv for your project. 25 | 26 | $ source django_env/bin/activate 27 | 28 | Install project dependencies: 29 | 30 | $ pip install -r requirements.txt 31 | 32 | Then simply apply the migrations: 33 | 34 | $ python manage.py migrate 35 | 36 | You can now run the development server: 37 | 38 | $ python manage.py runserver 39 | 40 | ## Project Structure 41 | 42 | A well-organized project structure is key to writing maintainable code. This section provides recommendations for organizing your Django project. 43 | 44 | ## Environment Configuration 45 | 46 | Managing environment variables is important for keeping your application secure and flexible. This section covers how to use environment variables in your Django project. 47 | 48 | ## PostgreSQL Database Connection 49 | 50 | Django provides a powerful ORM for interacting with databases. This section covers best practices for database management in Django. 51 | 52 | ## Security 53 | 54 | Security is a critical aspect of any web application. This section provides best practices for securing your Django application. 55 | 56 | ## Testing 57 | 58 | Testing is an important part of any software development process. This section covers best practices for testing Django applications. 59 | 60 | ## Deployment 61 | 62 | Deploying a Django application can be a complex process. This section provides best practices for deploying your Django application to production. 63 | 64 | ## Contributing 65 | 66 | Contributions to this repository are welcome! If you have suggestions for improving these best practices, feel free to open a pull request. 67 | 68 | ## License 69 | 70 | This repository is licensed under the MIT license. See LICENSE for details. 71 | 72 | ## Conclusion 73 | 74 | By following these best practices, you can create maintainable and scalable Django applications. Remember to keep learning and adapting as new best practices emerge. Happy coding! 75 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | volumes: 3 | postgres_data: 4 | 5 | 6 | services: 7 | app: 8 | build: . 9 | volumes: 10 | - .:/app 11 | ports: 12 | - 8000:8000 13 | container_name: django-api 14 | depends_on: 15 | - db 16 | command: python manage.py runserver 0.0.0.0:8000 --settings=settings.development 17 | db: 18 | image: postgres:13.0-alpine 19 | restart: always 20 | ports: 21 | - "5482:5432" 22 | volumes: 23 | - postgres_data:/var/lib/postgresql/data/ 24 | env_file: 25 | - ./.env 26 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$DATABASE" = "postgres" ] 4 | then 5 | echo "Waiting for postgres..." 6 | 7 | while ! nc -z $SQL_HOST $SQL_PORT; do 8 | sleep 0.1 9 | done 10 | 11 | echo "PostgreSQL started" 12 | fi 13 | 14 | python manage.py flush --settings=settings.development --no-input 15 | python manage.py migrate --settings=settings.development 16 | 17 | exec "$@" 18 | -------------------------------------------------------------------------------- /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", "settings.base") 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 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | alabaster==0.7.12 2 | asgiref==3.5.2 3 | astroid==2.12.11 4 | black==23.1.0 5 | bleach==6.0.0 6 | certifi==2022.9.24 7 | cfgv==3.3.1 8 | charset-normalizer==2.1.1 9 | click==8.1.3 10 | dill==0.3.5.1 11 | distlib==0.3.6 12 | Django==4.1.7 13 | django-cors-headers==3.14.0 14 | django-environ==0.9.0 15 | django-extensions==3.2.1 16 | django-filter==22.1 17 | django-reset-migrations==0.4.0 18 | djangorestframework==3.14.0 19 | docutils==0.19 20 | filelock==3.10.0 21 | gunicorn==20.1.0 22 | identify==2.5.21 23 | idna==3.4 24 | imagesize==1.4.1 25 | isort==5.10.1 26 | Jinja2==3.1.2 27 | lazy-object-proxy==1.7.1 28 | Markdown==3.4.1 29 | MarkupSafe==2.1.1 30 | mccabe==0.7.0 31 | mypy-extensions==0.4.3 32 | nodeenv==1.7.0 33 | packaging==23.0 34 | pathspec==0.10.1 35 | Pillow==9.4.0 36 | platformdirs==2.5.2 37 | pre-commit==3.2.0 38 | psycopg2-binary==2.9.5 39 | pycodestyle==2.9.1 40 | pydocstyle==6.1.1 41 | pyflakes==2.5.0 42 | Pygments==2.13.0 43 | pylint==2.15.4 44 | pyparsing==3.0.9 45 | pyscopg2==66.0.2 46 | pytz==2022.4 47 | PyYAML==6.0 48 | requests==2.28.1 49 | ruff==0.0.257 50 | six==1.16.0 51 | snowballstemmer==2.2.0 52 | sqlparse==0.4.3 53 | tomli==2.0.1 54 | tomlkit==0.11.5 55 | urllib3==1.26.12 56 | virtualenv==20.21.0 57 | webencodings==0.5.1 58 | wrapt==1.14.1 59 | -------------------------------------------------------------------------------- /ruff.toml: -------------------------------------------------------------------------------- 1 | # Never enforce `E501` (line length violations). 2 | ignore = ["E501"] 3 | 4 | # Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`. 5 | [per-file-ignores] 6 | "__init__.py" = ["E402"] 7 | "path/to/file.py" = ["E402"] 8 | -------------------------------------------------------------------------------- /sampleapp/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeryani/django-best-practices/491a58732391cef905dad4b2e9b0488aba2ee957/sampleapp/__init__.py -------------------------------------------------------------------------------- /sampleapp/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import Customer 4 | 5 | admin.site.register(Customer) 6 | -------------------------------------------------------------------------------- /sampleapp/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class SampleappConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "sampleapp" 7 | -------------------------------------------------------------------------------- /sampleapp/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.4 on 2022-12-24 18:23 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | initial = True 8 | 9 | dependencies = [] 10 | 11 | operations = [ 12 | migrations.CreateModel( 13 | name="Customer", 14 | fields=[ 15 | ( 16 | "id", 17 | models.BigAutoField( 18 | auto_created=True, 19 | primary_key=True, 20 | serialize=False, 21 | verbose_name="ID", 22 | ), 23 | ), 24 | ("name", models.CharField(max_length=200)), 25 | ("phone_number", models.CharField(max_length=20)), 26 | ], 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /sampleapp/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeryani/django-best-practices/491a58732391cef905dad4b2e9b0488aba2ee957/sampleapp/migrations/__init__.py -------------------------------------------------------------------------------- /sampleapp/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Customer(models.Model): 5 | name = models.CharField(max_length=200) 6 | phone_number = models.CharField(max_length=20) 7 | 8 | def __str__(self) -> str: 9 | return self.name 10 | -------------------------------------------------------------------------------- /sampleapp/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from .models import Customer 3 | 4 | 5 | class CustomerSerializer(serializers.ModelSerializer): 6 | class Meta: 7 | model = Customer 8 | fields = "__all__" 9 | -------------------------------------------------------------------------------- /sampleapp/tests.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeryani/django-best-practices/491a58732391cef905dad4b2e9b0488aba2ee957/sampleapp/tests.py -------------------------------------------------------------------------------- /sampleapp/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views 4 | 5 | urlpatterns = [ 6 | path("", views.apiOverview), 7 | path("customer_list/", views.customerList), 8 | path("customer_detail//", views.customerDetail), 9 | path("create_customer/", views.CreateCustomer.as_view()), 10 | ] 11 | -------------------------------------------------------------------------------- /sampleapp/views.py: -------------------------------------------------------------------------------- 1 | from rest_framework import permissions, status 2 | from rest_framework.decorators import api_view 3 | from rest_framework.generics import CreateAPIView 4 | from rest_framework.response import Response 5 | 6 | from .models import Customer 7 | from .serializers import CustomerSerializer 8 | 9 | 10 | @api_view(["GET"]) 11 | def apiOverview(request): 12 | api_overview = { 13 | "Customer List": "/customer_list/", 14 | "Detail Customer View": "/customer_detail//", 15 | "Create Customer": "/create_customer/", 16 | } 17 | return Response(api_overview) 18 | 19 | 20 | @api_view(["GET"]) 21 | def customerList(request): 22 | customers = Customer.objects.all() 23 | serializer = CustomerSerializer(customers, many=True) 24 | return Response(serializer.data) 25 | 26 | 27 | @api_view(["GET"]) 28 | def customerDetail(request, pk): 29 | customer = Customer.objects.get(id=pk) 30 | serializer = CustomerSerializer(customer, many=False) 31 | return Response(serializer.data) 32 | 33 | 34 | class CreateCustomer(CreateAPIView): 35 | queryset = Customer.objects.all() 36 | serializer = CustomerSerializer 37 | permission_classes = [permissions.AllowAny] 38 | 39 | def post(self, request, format=None): 40 | serializer = CustomerSerializer(data=request.data) 41 | if serializer.is_valid(): 42 | serializer.save() 43 | return Response(serializer.data, status=status.HTTP_201_CREATED) 44 | return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 45 | 46 | def get_serializer_class(self): 47 | return CustomerSerializer 48 | -------------------------------------------------------------------------------- /sampleproject/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeryani/django-best-practices/491a58732391cef905dad4b2e9b0488aba2ee957/sampleproject/__init__.py -------------------------------------------------------------------------------- /sampleproject/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for boilerplateproject 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/4.1/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", "sampleproject.settings") 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /sampleproject/urls.py: -------------------------------------------------------------------------------- 1 | """boilerplateproject URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/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 | 19 | urlpatterns = [ 20 | path("admin/", admin.site.urls), 21 | path("", include("sampleapp.urls")), 22 | ] 23 | -------------------------------------------------------------------------------- /sampleproject/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for boilerplateproject 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/4.1/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", "sampleproject.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /settings/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeryani/django-best-practices/491a58732391cef905dad4b2e9b0488aba2ee957/settings/__init__.py -------------------------------------------------------------------------------- /settings/base.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for boilerplateproject project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | import os 13 | from pathlib import Path 14 | 15 | import environ 16 | 17 | # Initialise environment variables 18 | env = environ.Env(DEBUG=(bool, True)) 19 | environ.Env.read_env() 20 | DEBUG = int(os.environ.get("DEBUG", default=0)) 21 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 22 | BASE_DIR = Path(__file__).resolve().parent.parent 23 | 24 | 25 | PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) 26 | # Quick-start development settings - unsuitable for production 27 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 28 | 29 | # SECURITY WARNING: keep the secret key used in production secret! 30 | SECRET_KEY = env("SECRET_KEY") 31 | 32 | ALLOWED_HOSTS = ["*"] 33 | 34 | 35 | # Application definition 36 | 37 | INSTALLED_APPS = [ 38 | "rest_framework", 39 | "sampleapp.apps.SampleappConfig", 40 | "django.contrib.admin", 41 | "django.contrib.auth", 42 | "django.contrib.contenttypes", 43 | "django.contrib.sessions", 44 | "django.contrib.messages", 45 | "django.contrib.staticfiles", 46 | ] 47 | 48 | MIDDLEWARE = [ 49 | "django.middleware.security.SecurityMiddleware", 50 | "django.contrib.sessions.middleware.SessionMiddleware", 51 | "django.middleware.common.CommonMiddleware", 52 | "django.middleware.csrf.CsrfViewMiddleware", 53 | "django.contrib.auth.middleware.AuthenticationMiddleware", 54 | "django.contrib.messages.middleware.MessageMiddleware", 55 | "django.middleware.clickjacking.XFrameOptionsMiddleware", 56 | ] 57 | 58 | ROOT_URLCONF = "sampleproject.urls" 59 | 60 | TEMPLATES = [ 61 | { 62 | "BACKEND": "django.template.backends.django.DjangoTemplates", 63 | "DIRS": [], 64 | "APP_DIRS": True, 65 | "OPTIONS": { 66 | "context_processors": [ 67 | "django.template.context_processors.debug", 68 | "django.template.context_processors.request", 69 | "django.contrib.auth.context_processors.auth", 70 | "django.contrib.messages.context_processors.messages", 71 | ], 72 | }, 73 | }, 74 | ] 75 | 76 | WSGI_APPLICATION = "sampleproject.wsgi.application" 77 | 78 | 79 | # Database 80 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 81 | 82 | DATABASES = { 83 | "default": { 84 | "ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"), 85 | "NAME": os.environ.get("SQL_DATABASE", BASE_DIR / "db.sqlite3"), 86 | "USER": os.environ.get("SQL_USER", "user"), 87 | "PASSWORD": os.environ.get("SQL_PASSWORD", "password"), 88 | "HOST": os.environ.get("DATABASE_HOST", "localhost"), 89 | "PORT": os.environ.get("SQL_PORT", "5432"), 90 | } 91 | } 92 | 93 | # Password validation 94 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 95 | 96 | AUTH_PASSWORD_VALIDATORS = [ 97 | { 98 | "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", 99 | }, 100 | { 101 | "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", 102 | }, 103 | { 104 | "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", 105 | }, 106 | { 107 | "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", 108 | }, 109 | ] 110 | 111 | # Internationalization 112 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 113 | 114 | LANGUAGE_CODE = "en-us" 115 | 116 | TIME_ZONE = "UTC" 117 | 118 | USE_I18N = True 119 | 120 | USE_TZ = True 121 | 122 | 123 | # Static files (CSS, JavaScript, Images) 124 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 125 | 126 | STATIC_URL = "static/" 127 | 128 | # Default primary key field type 129 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 130 | 131 | DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" 132 | -------------------------------------------------------------------------------- /settings/development.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for boilerplateproject project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | import os 13 | from pathlib import Path 14 | 15 | import environ 16 | 17 | # Initialise environment variables 18 | env = environ.Env(DEBUG=(bool, True)) 19 | environ.Env.read_env() 20 | DEBUG = int(os.environ.get("DEBUG", default=0)) 21 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 22 | BASE_DIR = Path(__file__).resolve().parent.parent 23 | 24 | 25 | PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) 26 | # Quick-start development settings - unsuitable for production 27 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 28 | 29 | # SECURITY WARNING: keep the secret key used in production secret! 30 | SECRET_KEY = env("SECRET_KEY") 31 | 32 | ALLOWED_HOSTS = ["*"] 33 | 34 | 35 | # Application definition 36 | 37 | INSTALLED_APPS = [ 38 | "rest_framework", 39 | "sampleapp.apps.SampleappConfig", 40 | "django.contrib.admin", 41 | "django.contrib.auth", 42 | "django.contrib.contenttypes", 43 | "django.contrib.sessions", 44 | "django.contrib.messages", 45 | "django.contrib.staticfiles", 46 | ] 47 | 48 | MIDDLEWARE = [ 49 | "django.middleware.security.SecurityMiddleware", 50 | "django.contrib.sessions.middleware.SessionMiddleware", 51 | "django.middleware.common.CommonMiddleware", 52 | "django.middleware.csrf.CsrfViewMiddleware", 53 | "django.contrib.auth.middleware.AuthenticationMiddleware", 54 | "django.contrib.messages.middleware.MessageMiddleware", 55 | "django.middleware.clickjacking.XFrameOptionsMiddleware", 56 | ] 57 | 58 | ROOT_URLCONF = "sampleproject.urls" 59 | 60 | TEMPLATES = [ 61 | { 62 | "BACKEND": "django.template.backends.django.DjangoTemplates", 63 | "DIRS": [], 64 | "APP_DIRS": True, 65 | "OPTIONS": { 66 | "context_processors": [ 67 | "django.template.context_processors.debug", 68 | "django.template.context_processors.request", 69 | "django.contrib.auth.context_processors.auth", 70 | "django.contrib.messages.context_processors.messages", 71 | ], 72 | }, 73 | }, 74 | ] 75 | 76 | WSGI_APPLICATION = "sampleproject.wsgi.application" 77 | 78 | 79 | # Database 80 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 81 | 82 | DATABASES = { 83 | "default": { 84 | "ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"), 85 | "NAME": os.environ.get("SQL_DATABASE", BASE_DIR / "db.sqlite3"), 86 | "USER": os.environ.get("SQL_USER", "user"), 87 | "PASSWORD": os.environ.get("SQL_PASSWORD", "password"), 88 | "HOST": os.environ.get("SQL_HOST", "localhost"), 89 | "PORT": os.environ.get("SQL_PORT", "5432"), 90 | } 91 | } 92 | 93 | # Password validation 94 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 95 | 96 | AUTH_PASSWORD_VALIDATORS = [ 97 | { 98 | "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", 99 | }, 100 | { 101 | "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", 102 | }, 103 | { 104 | "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", 105 | }, 106 | { 107 | "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", 108 | }, 109 | ] 110 | 111 | # Internationalization 112 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 113 | 114 | LANGUAGE_CODE = "en-us" 115 | 116 | TIME_ZONE = "UTC" 117 | 118 | USE_I18N = True 119 | 120 | USE_TZ = True 121 | 122 | 123 | # Static files (CSS, JavaScript, Images) 124 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 125 | 126 | STATIC_URL = "static/" 127 | 128 | # Default primary key field type 129 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 130 | 131 | DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" 132 | -------------------------------------------------------------------------------- /settings/production.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for boilerplateproject project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | --------------------------------------------------------------------------------