[Django]-How to update django template variable without reloading page (AJAX)?

3๐Ÿ‘

โœ…

I think you have a bit of a misconception about how your django application communicates with your client. You have to understand the request response lifecycle.

Your template context does only exist inside the request response lifecycle. Every request from your client will start a new worker with a new template context to generate a response. There is no persistent stateful connection between your client application and django.

That means that if you want to update your client side page without reloading it (requesting it again) you have to pass the new card count with your JsonResponse to the client and let the javascript code that handles the response update the displayed value.

๐Ÿ‘คtrixn

2๐Ÿ‘

You can not. Once the page have been rendered, all template variables disappear. You can use an input hidden to store the cart.count value, and then update that value.

๐Ÿ‘คafilardo

Leave a comment