{"id":8954,"library":"django-request-id","title":"Django Request ID","description":"django-request-id is a Python library for Django that augments each HTTP request with a unique ID, primarily for improved logging and tracing across services. It is currently at version 1.0.0 and maintains a stable release cadence, focusing on core functionality and compatibility with recent Django versions.","status":"active","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/nigma/django-request-id","tags":["django","logging","middleware","request-id","tracing"],"install":[{"cmd":"pip install django-request-id","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"This is a Django middleware library, requiring Django to function.","package":"Django","optional":false}],"imports":[{"note":"The top-level package for imports is `request_id`, not `django_request_id`.","wrong":"from django_request_id.middleware import RequestIDMiddleware","symbol":"RequestIDMiddleware","correct":"from request_id.middleware import RequestIDMiddleware"},{"note":"The top-level package for imports is `request_id`, not `django_request_id`.","wrong":"from django_request_id.logging import RequestIDFilter","symbol":"RequestIDFilter","correct":"from request_id.logging import RequestIDFilter"}],"quickstart":{"code":"import os\n\n# settings.py\n\n# Add the middleware to your Django project\nMIDDLEWARE = [\n    # ... other middleware (e.g., 'django.middleware.security.SecurityMiddleware')\n    'request_id.middleware.RequestIDMiddleware',\n    # ... other middleware (e.g., 'django.contrib.sessions.middleware.SessionMiddleware')\n]\n\n# Configure logging to include the request ID\nLOGGING = {\n    'version': 1,\n    'disable_existing_loggers': False,\n    'filters': {\n        'request_id': {\n            '()': 'request_id.logging.RequestIDFilter'\n        }\n    },\n    'formatters': {\n        'verbose': {\n            'format': '{levelname} {asctime} {request_id} {module} {process:d} {thread:d} {message}',\n            'style': '{',\n        },\n    },\n    'handlers': {\n        'console': {\n            'class': 'logging.StreamHandler',\n            'filters': ['request_id'], # Apply the filter to a handler\n            'formatter': 'verbose',\n        }\n    },\n    'root': {\n        'handlers': ['console'],\n        'level': 'INFO',\n    },\n    'loggers': {\n        'django': {\n            'handlers': ['console'],\n            'level': 'INFO',\n            'propagate': True,\n        },\n    },\n}\n\n# Example usage in a Django view or signal (for testing, not part of quickstart setup itself)\n# import logging\n# logger = logging.getLogger(__name__)\n# def my_view(request):\n#     logger.info('This message should have a request ID.')\n#     return HttpResponse('Hello world')","lang":"python","description":"To quickly integrate `django-request-id`, add `request_id.middleware.RequestIDMiddleware` to your `MIDDLEWARE` setting. Crucially, configure your Django `LOGGING` settings to include `request_id.logging.RequestIDFilter` and apply it to your desired log handlers. Also, ensure your log formatters include `{request_id}` to display the unique ID."},"warnings":[{"fix":"Ensure `LOGGING['filters']['request_id']['()'] = 'request_id.logging.RequestIDFilter'` is set and `'filters': ['request_id']` is applied to your `LOGGING['handlers']` entries. Also, modify your formatter `format` strings to include `{request_id}`.","message":"The request ID will not appear in your logs unless you explicitly configure the logging system to use `RequestIDFilter` and include `{request_id}` in your formatter strings.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consult your proxy/load balancer documentation (e.g., Nginx, AWS ELB/ALB) to understand how `X-Request-ID` headers are handled and configure them to either pass through or strip/override as per your requirements.","message":"When integrating with upstream proxies or load balancers, be aware that `django-request-id` will use an existing `X-Request-ID` header if present. If you need it to always generate a new ID, you might need to ensure the header is not passed from the proxy or configure the proxy to override it.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Add `REQUEST_ID_HEADER = 'Your-Custom-Header'` and `REQUEST_ID_RESPONSE_HEADER = 'Your-Custom-Header'` to your `settings.py`.","message":"The default header used for injecting/extracting the request ID is `X-Request-ID`. If your infrastructure uses a different header name, you'll need to configure `REQUEST_ID_HEADER` and `REQUEST_ID_RESPONSE_HEADER` in your Django settings.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the library using `pip install django-request-id`. Verify that `request_id.middleware.RequestIDMiddleware` is correctly spelled and listed in your `settings.MIDDLEWARE`.","cause":"The `django-request-id` library is either not installed or the middleware path in `settings.py` is incorrect.","error":"ModuleNotFoundError: No module named 'request_id.middleware'"},{"fix":"In `settings.py`, ensure your `LOGGING` configuration includes a filter like `LOGGING['filters']['request_id'] = {'()': 'request_id.logging.RequestIDFilter'}`. Then, apply this filter to your handler, e.g., `LOGGING['handlers']['console']['filters'] = ['request_id']`. Finally, make sure your formatter `format` string contains `{request_id}`.","cause":"The `RequestIDFilter` is not correctly applied to your logging handler, or your formatter string does not include `{request_id}`.","error":"Request ID not appearing in my Django logs"},{"fix":"Ensure that `request_id.logging.RequestIDFilter` is correctly configured and applied to the specific logging handler(s) that process the logs you are inspecting or modifying.","cause":"This error typically occurs if you're trying to access `record.request_id` within a custom logging filter or handler, but the `RequestIDFilter` (which adds this attribute) has not been applied to the `LogRecord`.","error":"AttributeError: 'LogRecord' object has no attribute 'request_id'"}]}