Middleware API Documentation

Django Vibe provides middleware components that enhance Django's request/response processing with Material Design-specific features.

SiteMiddleware

The SiteMiddleware processes site and app permissions based on URL patterns and adds context data to template responses.

Automatic Configuration

When using the default app configuration ('material' in INSTALLED_APPS), this middleware is automatically added to your middleware stack.

The middleware is automatically added through the MaterialConfig default app configuration. This means you don't need to manually add it to your MIDDLEWARE setting.

Manual Configuration

If you prefer to manually configure the middleware, you can use the MaterialManualConfig:

# In settings.py
INSTALLED_APPS = [
    # Replace 'material' with:
    'material.apps.MaterialManualConfig',
    # ...
]

MIDDLEWARE = [
    # ...
    'material.middleware.SiteMiddleware',
    # ...
]

How It Works

The middleware performs the following functions:

  1. Checks if the user has permission to access the current site and app
  2. Adds site and app objects to the request.resolver_match for view access
  3. Injects app context data into template responses

Permission Checking

The middleware checks for has_view_permission(user) methods on both site and app objects that are provided in the URL pattern's extra dictionary. If permissions are not granted, a PermissionDenied exception is raised.

# Example URL pattern with site and app objects
from material.urls import Site, Application

app = Application(
    title="My App",
    app_name="myapp",
    # ...
)

site = Site(
    title="My Site",
    viewsets=[app],
    # ...
)

urlpatterns = [
    path('', site.urls),  # SiteMiddleware will check permissions
]

Context Data

If an app object has a get_context_data(request) method, the middleware will call it and add the returned data to the template response context.