django-cid

raw JSON →
3.0 verified Fri May 01 auth: no python

Adds correlation IDs (CID) to Django requests for tracing and debugging. Currently at version 3.0, requires Python >=3.8. Active development with periodic releases.

pip install django-cid
error ImportError: cannot import name 'CIDMiddleware' from 'cid.middleware'
cause Middleware renamed to CorrelationIdMiddleware in v3.0.
fix
Use 'from cid.middleware import CorrelationIdMiddleware' and add 'cid.middleware.CorrelationIdMiddleware' to MIDDLEWARE.
error ImportError: cannot import name 'get_cid' from 'cid.utils'
cause get_cid moved to cid top-level in v3.0.
fix
Use 'from cid import get_cid' instead.
error AttributeError: 'NoneType' object has no attribute 'split'
cause get_cid returns None if the middleware hasn't run yet (wrong middleware order) or if the request lacks a CID header.
fix
Check that CorrelationIdMiddleware is placed early in MIDDLEWARE and that incoming requests include a valid CID header (e.g., X-Correlation-ID).
breaking Middleware renamed from CIDMiddleware to CorrelationIdMiddleware in v3.0. Old imports will cause ImportError.
fix Replace 'cid.middleware.CIDMiddleware' with 'cid.middleware.CorrelationIdMiddleware' in MIDDLEWARE settings.
breaking get_cid function moved from cid.utils to cid top-level. Old import path broken.
fix Change 'from cid.utils import get_cid' to 'from cid import get_cid'.
deprecated The 'cid' template tag and context processor were removed in v3.0. Use get_cid directly in views or templates via template context.
fix Pass cid to template context manually: context['cid'] = get_cid(request).
gotcha CorrelationIdMiddleware must be placed before other middlewares that need to access the CID, otherwise get_cid will return None.
fix Ensure CorrelationIdMiddleware is early in the MIDDLEWARE list (e.g., after CommonMiddleware but before SessionMiddleware).

Basic setup: add middleware and use get_cid() to retrieve the correlation ID.

import os

# Add to MIDDLEWARE in settings:
MIDDLEWARE = [
    'cid.middleware.CorrelationIdMiddleware',
    # ...
]

# Then in views:
from cid import get_cid

def my_view(request):
    cid = get_cid(request)
    return HttpResponse(f'Correlation ID: {cid}')