Why request.user.is_authenticated sync?

Getting django.core.exceptions.SynchronousOnlyOperation while running:

async def index(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('account:login') + f'?next={request.path}')

Why it sync?

Welcome @DragonwolfAside !

The request.user object is lazy. It is not fully populated when assigned to the request. So, the first request to request.user is going to require the query be executed to retrieve that field.

1 Like

Use auser() Request and response objects | Django documentation | Django

Something like:

if (await request.auser()).is_authenticated:
    …
4 Likes