Skip to main content

A Django project setup automation tool

Project description

Django Setup (django_setup)

A command-line utility that simplifies Django project initialization and application management.

Features

  • Streamlined Project Initialization: Create a new Django project with environment variables support, sensible defaults, and a centralized app configuration system.
  • Simplified App Creation: Add new apps to your project with automatic registration in URLs and settings.
  • Centralized App Management: Manage all your project apps from a single configuration file.

Installation

pip install djangosetupheist

Usage

Initialize a New Django Project

djs init [app_name]

If app_name is not provided, you will be prompted to enter one.

This command will:

  • Create a new Django project with the specified name
  • Set up a .env file for environment variables
  • Configure settings.py to use environment variables for:
    • SECRET_KEY
    • ALLOWED_HOSTS
    • Other configurable settings
  • Create an appsConfig.py file to centralize app registration

Create a New App

djs startapp [app_name]

This command will:

  • Create a new Django app with the specified name
  • Add the app to appsConfig.py with default values:
    {
        'app_name': 'app_name',
        'url': 'app_name/',
        'namespace': 'app_name'
    }
    
  • The app will be automatically registered in INSTALLED_APPS and URL patterns

appsConfig.py Structure

The appsConfig.py file is the central configuration point for all your apps. It contains a list of dictionaries with the following structure:

app_configs = [
    {
        'app_name': 'items',      # The name of the app
        'url': 'items/',          # The URL prefix for this app
        'namespace': 'items'      # The namespace for this app's URLs
    },
    # More apps...
]

Customizing App Configuration

You can modify the values in appsConfig.py to change how your apps are configured:

  • app_name: The name of your app (should match the directory name)
  • url: The URL prefix for this app in your project's URL configuration
  • namespace: The namespace used for URL names

Important: If you change the namespace, make sure to update the app's urls.py file to match the new namespace.

Benefits

  • Single Source of Truth: Manage all your apps from a single configuration file
  • Reduced Boilerplate: No need to manually update settings.py and urls.py when adding new apps
  • Environment Variable Support: Built-in support for environment variables through .env file
  • Consistent Structure: Maintain a consistent project structure across all your Django projects

Example

After initializing a project with djs init myproject, your project structure will include:

myproject/
├── manage.py
├── myproject/
│   ├── assets/
│   │   ├── media/
│   │   ├── static/
│   ├── templates/
│   ├── .env
│   ├── .envtemp
│   ├── __init__.py
│   ├── appsConfig.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py

The appsConfig.py file will contain:

from django.urls import path, include

app_configs = [
    # { "app_name": "finances.payments", "url": "finances/payments", "namespace": "payments" },
]

def getAppUrls():
    urlpatterns = []
    for config in app_configs:
        urlpatterns.append(
            path(f"{config['url']}", include(f"{config['app_name']}.urls", namespace=config['namespace']))
        )
    return urlpatterns

def getAppNames():
    return [config['app_name'] for config in app_configs]

After running djs startapp items, the structure will be updated:

myproject/
├── .env
├── items/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations/
│   ├── templates/
│   │   ├── items/
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── myproject/
│   ├── assets/
│   │   ├── media/
│   │   ├── static/
│   ├── templates/
│   ├── .env
│   ├── .envtemp
│   ├── __init__.py
│   ├── appsConfig.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py

And appsConfig.py will be updated to include:

app_configs = [
    {
        'app_name': 'items',
        'url': 'items/',
        'namespace': 'items'
    }
]

Other Files

settings.py

# Settings for store using django_setup
from pathlib import Path
import os, environ
from .appsConfig import getAppNames

# Uses environ to interface with environment variables
env = environ.Env(
    DEBUG=(bool, False)
)
environ.Env.read_env() 

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env('DEBUG')

ALLOWED_HOSTS = env.list('ALLOWED_HOSTS')
INTERNAL_IPS = env.list('INTERNAL_IPS')

# CORS: Requires django-cors-headers
# CORS_ALLOWED_ORIGINS = env.list('CORS_ALLOWED_ORIGINS')
# CORS_ALLOW_ALL_ORIGINS = True


# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]
INSTALLED_APPS += getAppNames()

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "store.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / 'store/templates'],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "store.wsgi.application"


# Database: using environment variables for both production and development
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': os.getenv("DB_ENGINE"),
        'NAME': os.getenv("DB_NAME"),
        'USER': os.getenv("DB_USER"),
        'PASSWORD': os.getenv("DB_PASSWORD"),
        'HOST': os.getenv("DB_HOST"),
        # 'PORT': os.getenv("DB_PORT"),
        "OPTIONS": {}
    }
}

# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]


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

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Secure Cookies
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = "static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'store/assets/static/'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'store/assets/staticfiles')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'store/assets/media')

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Email settings
EMAIL_BACKEND = env('EMAIL_BACKEND')
EMAIL_HOST = env('EMAIL_HOST')
EMAIL_PORT = env('EMAIL_PORT')
EMAIL_USE_TLS = env('EMAIL_USE_TLS')
EMAIL_HOST_USER = env('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD')
DEFAULT_FROM_EMAIL = env('DEFAULT_FROM_EMAIL')
TO_EMAILS = env.list('TO_EMAILS')

main urls.py

from django.contrib import admin
from django.urls import path, include

from .appsConfig import getAppUrls

urlpatterns = [
    path("admin/", admin.site.urls),
]

urlpatterns += getAppUrls()

.env with an autogenerated secret key

DEBUG=True
SECRET_KEY=x%kneIyp2Hk^l*[iaM1^SG<SB#V{owr@
ALLOWED_HOSTS=127.0.0.1,localhost

CORS_ALLOWED_ORIGINS=http://127.0.0.1,http://localhost
INTERNAL_IPS=127.0.0.1,localhost

APP_ROOT=.

DB_ENGINE='django.db.backends.sqlite3'
DB_NAME='db.sqlite3'
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=
DB_STRICT_MYSQL=


EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend'
EMAIL_HOST=''
EMAIL_PORT=
EMAIL_HOST_USER=''
EMAIL_HOST_PASSWORD=
EMAIL_USE_TLS=True
DEFAULT_FROM_EMAIL=''
TO_EMAILS=

# azure blob config
AZURE_ACCOUNT_NAME=''
AZURE_CONNECTION_STRING=''
AZURE_ACCOUNT_KEY=''
AZURE_URL_EXPIRATION_SECS=
AZURE_MEDIA_CONTAINER=''
AZURE_STATIC_CONTAINER=''

License

GNU General Public License v3 or later (GPL-3.0+)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangosetupheist-0.6.2.tar.gz (28.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djangosetupheist-0.6.2-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

Details for the file djangosetupheist-0.6.2.tar.gz.

File metadata

  • Download URL: djangosetupheist-0.6.2.tar.gz
  • Upload date:
  • Size: 28.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djangosetupheist-0.6.2.tar.gz
Algorithm Hash digest
SHA256 8702de7aa33a77b67740f8c2e29d61d77391597128e4a499ba7ddd3356dd415c
MD5 9560cc266d70d1dbb0ab331916c026a7
BLAKE2b-256 01c1ea2ded66f63fcb4a45c926c3abced148d3b945cb1b0306a1b66ef56be7c1

See more details on using hashes here.

File details

Details for the file djangosetupheist-0.6.2-py3-none-any.whl.

File metadata

File hashes

Hashes for djangosetupheist-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0538c98bb5ac32d6cf7c235cc8df871a8e67ff1dd56f7900bfd2d6d3eb252327
MD5 ab5ff7057dd0f34742063370db7efa77
BLAKE2b-256 0b34f48b01cca56fe3983e07381aaef022d9288fe0ff0d907d7b99fb76f67ba7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page