Package: django-middleware-request-id
django-middleware-request-idThe middleware detect if a client or the front reverse proxy server provides a X-Request-ID header, and get it as the request_id. If no such header is provided, it can provide a random value.Installpip install django-middleware-request-idUsagepro/settings.pyINSTALLED_APPS = [
    "django_middleware_global_request",
    "django_middleware_request_id",
]

MIDDLEWARE = [
    ...
    "django_middleware_global_request.middleware.GlobalRequestMiddleware",
    "django_middleware_request_id.middlewares.DjangoMiddlewareRequestId",
    ...
]app/views.pyfrom django.http import HttpResponse
from django_middleware_request_id import get_request_id

def get_request_id_view(request):
    request_id = get_request_id()
    return HttpResponse(request_id)app/urls.pyfrom django.contrib import admin
from django.urls import path
froml . import views

urlpatterns = [
    path('get_request_id', views.get_request_id_view),
]Set the request id at nginxhttp {
    ...
    # -------------------------------------------------------------------------------
    # Set variable $reqid.
    # If you trust client or front nginx's header, you can use `map block` here.
    # -------------------------------------------------------------------------------
    map $http_x_request_id $reqid {
        default $http_x_request_id;
        "" $request_id;
    }
    # -------------------------------------------------------------------------------

    server {

        # -------------------------------------------------------------------------------
        # If you don't trust client and front nginx's header, or you are sure this 
        # is the first front nginx, just set the variable $reqid to a new random value.
        # Use `map block` above, or use `set line` below, but don't use both.
        # Uncomment the `set line` below to `set new random value`.
        # -------------------------------------------------------------------------------
        # set $reqid $request_id;
        # -------------------------------------------------------------------------------

        ...
        location /api/ {
            proxy_pass http://backend/api/;
            proxy_set_header X-Request-Id $reqid;
            ...
        }
        ...
    }
}Test Passedpython27:~=django1.11.29python34:~=django1.11.29python34:~=django2.0.13python35:~=django1.11.29python35:~=django2.0.13python35:~=django2.1.15python35:~=django2.2.28python36:~=django2.0.13python36:~=django2.1.15python36:~=django2.2.28python36:~=django3.0.14python36:~=django3.1.14python36:~=django3.2.21python37:~=django2.0.13python37:~=django2.1.15python37:~=django2.2.28python37:~=django3.0.14python37:~=django3.1.14python37:~=django3.2.21python38:~=django2.0.13python38:~=django2.1.15python38:~=django2.2.28python38:~=django3.0.14python38:~=django3.1.14python38:~=django3.2.21python38:~=django4.0.10python38:~=django4.1.11python38:~=django4.2.5python39:~=django2.0.13python39:~=django2.1.15python39:~=django2.2.28python39:~=django3.0.14python39:~=django3.1.14python39:~=django3.2.21python39:~=django4.0.10python39:~=django4.1.11python39:~=django4.2.5python310:~=django2.1.15python310:~=django2.2.28python310:~=django3.0.14python310:~=django3.1.14python310:~=django3.2.21python310:~=django4.0.10python310:~=django4.1.11python310:~=django4.2.5python311:~=django2.2.28python311:~=django3.0.14python311:~=django3.1.14python311:~=django3.2.21python311:~=django4.0.10python311:~=django4.1.11python311:~=django4.2.5Releasesv0.1.0First release.v0.1.2Fix problem that importget_request_idfrom the package root.Fix problem that NOT using DJANGO_REQUEST_ID_HEADER setting.Fix problem that call get_request_id() from none request context.v0.1.3Doc update.v0.1.4Unit test all passed.