Django Integration¶
-
Automatic Dependency Management
Inject dependencies in routes and automatically manage container lifecycle.
-
Request Objects
Use Django request in Wireup dependencies.
-
Django Settings
The integration exposes Django settings to Wireup as config.
-
Shared business logic
Wireup is framework-agnostic. Share the service layer between web applications and other interfaces, such as a CLI.
Quick Start¶
This is the shortest path to a working endpoint with explicit injection.
settings.py
from wireup.integration.django import WireupSettings
INSTALLED_APPS = [
# ...existing apps...
"wireup.integration.django",
]
MIDDLEWARE = [
"wireup.integration.django.wireup_middleware",
# ...existing middleware...
]
WIREUP = WireupSettings(
injectables=["mysite.services"],
auto_inject_views=False,
)
mysite/services.py
from wireup import injectable
@injectable
class GreeterService:
def greet(self, name: str) -> str:
return f"Hello {name}"
mysite/views.py
from django.http import HttpRequest, HttpResponse
from wireup import Injected
from wireup.integration.django import inject
from mysite.services import GreeterService
@inject
def greet(
request: HttpRequest, greeter: Injected[GreeterService]
) -> HttpResponse:
return HttpResponse(greeter.greet(request.GET.get("name", "World")))
mysite/urls.py
from django.urls import path
from mysite.views import greet
urlpatterns = [
path("greet/", greet),
]
Run the app with python manage.py runserver, then open http://127.0.0.1:8000/greet/?name=World.
Detailed Guides¶
- Django Setup and Installation: installation, middleware placement, and settings/config integration.
- Inject in Views: core Django, DRF, Ninja, forms, and request-scoped patterns.
- Request Lifecycle Patterns: reusable decorators, middleware-adjacent hooks, and direct container access.
- App-Level Injection: management commands, signals, checks, and scripts with
@inject_app. - Django Testing:
Client,AsyncClient,call_command, and override patterns. - Troubleshooting: common errors and quick fixes.