simonw / djp

A plugin system for Django
https://djp.readthedocs.io
Apache License 2.0
90 stars 2 forks source link

Simplify settings.py configuration #7

Closed simonw closed 2 months ago

simonw commented 2 months ago

Currently you have to do this:

import djp

# ...

INSTALLED_APPS = [
    "your_app1",
    "your_app2",
] + djp.installed_apps()

# ...

MIDDLEWARE = djp.middleware([
    "your_middleware1",
    "your_middleware2",
])

# And at the very end of that file:
djp.settings(globals())

That djp.settings(globals()) line at the end could automatically do the things that the djp.installed_apps() and djp.middleware()` lines do.

User would still need to modify urls.py but simplifying setup in this way would help avoid accidental misconfiguration and make things easier.

simonw commented 2 months ago

Tried this out and it's pretty nice. Here's the settings.py for the tests in that branch:

import djp

SECRET_KEY = "django-insecure-test-key"
DEBUG = True
ALLOWED_HOSTS = ["*"]

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
]

MIDDLEWARE = []

ROOT_URLCONF = "tests.test_project.urls"

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": ":memory:",
    }
}

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "APP_DIRS": True,
    }
]

USE_TZ = True

djp.settings(globals())