User /cart uses in every template html (navbar)

I need to show the user information and cart information in the navbar. Do I need to call this in every template render. Or can we use it by making a glabal definition (like a context processor in django)?

For example:

async def set_global_key(request):
    request.ctx.session['global_key'] = 'global_value'

app.register_middleware(set_global_key, 'request')

in template:

{{ request.ctx.session.global_key }}

gives error: ‘request’ is undefined

Is it ok to use all render template as below?

user = request.ctx.user
cart = Cart().all()
context = {"user": user, "cart": cart}
return await render("home.html", context=context, status=200)

Sorry for the late reply. You can certainly add request into your templates.

async def setup_request_context(request: Request):["request"] = request


1 Like