Search code examples
djangodjango-context

Is content from AJAX call added to Django context variable


I am using the JQuery load function to load part of my page. Can I access the variables from that page in the page that loads it. e.g.

  • Page A uses JQuery load function to load B

  • Page B loads and sets a variable in context called pageB_var which holds a django object

  • Page A can then access this variable by doing {{pageB_var}} since it was added to the context

If not what is the best way of doing this?

Thanks


Solution

  • It sounds like you're using an asynchronous request to update part of a page, receiving a partial HTML response and replacing part of the markup of your page with this response.

    You've found that this obviously limits you to updating information contained within that partial page.

    Consider instead providing a JSON response to your asynchronous request, containing all the information you need, and then updating the necessary HTML in any part of the page by manipulating the DOM client-side, with JavaScript. Think of the JSON as being your context for this purpose.