# view is not always executed

**URL:** <https://forum.djangoproject.com/t/view-is-not-always-executed/31398>\
**Category:** Using Django\
**Created:** [May 22, 2024, 6:51am UTC](https://forum.djangoproject.com/t/view-is-not-always-executed/31398 "2024-05-22T06:51:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhaozZ](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahaozz/32/18253_2.png) [@AhaozZ](https://forum.djangoproject.com/u/AhaozZ)\
**Post date:** [May 22, 2024, 6:51am UTC](https://forum.djangoproject.com/t/view-is-not-always-executed/31398/1 "2024-05-22T06:51:57Z")

</div>

request will be recorded by the middleware and executed in async view, but in some cases, it will not be executed in view.

### Settings:

Python: 3.10.12

Django: 4.2.1

### middleware settings:

```auto
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    ...
    "utils.middleware.RequestMiddleware",
]

```

### RequestMiddleware:

```auto
class RequestMiddleware(MiddlewareMixin):
    def process_view(self, request, view_func, view_args, view_kwargs):
        req_str = "reqeust user:{} request method:{} request path:{} request body:{}".format(
            request.user,
            request.META["REQUEST_METHOD"],
            request.path,
            request.body
        )
        logger.info(req_str)
        return None

    def process_response(self, request, response):
        data = response.content
        data = data.decode("utf-8")
        logger.info("response status code:{} data:{}".format(response.status_code, data))
        return response

    def process_exception(self, request, exception):
        logger.error(traceback.format_exc())

```

### views.py:

```auto
async def report(request):
    my_views.delay() # celery task
    return send_message({"result": 0})

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 22, 2024, 12:00pm UTC](https://forum.djangoproject.com/t/view-is-not-always-executed/31398/2 "2024-05-22T12:00:32Z")

</div>

Welcome @AhaozZ !

Can you be more specific and provide more details about the issue you’re facing here? I’m not sure I understand what you’re asking or what information you’re looking for from us.

---

<div class="post-metadata">

**Author:** ![AhaozZ](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahaozz/32/18253_2.png) [@AhaozZ](https://forum.djangoproject.com/u/AhaozZ)\
**Post date:** [May 23, 2024, 3:04am UTC](https://forum.djangoproject.com/t/view-is-not-always-executed/31398/3 "2024-05-23T03:04:48Z")

</div>

Hi Ken!  
In prod environment, `RequestMiddleware` can log information for each request, but having an async view does not always work. Most of the time, it can work normally, but sometimes it may not work. I cannot reproduce this situation when using jemter for testing in the testing environment.  
I try to log something in this view:

```auto
async def report(request):
    logger.info(f"request:{request.body} start work")
    my_views.delay() # celery task
    return send_message({"result": 0})

```

There are some requests in the log that have not been recorded.
