# How to update the dictionary in django templates using Ajax?

**URL:** <https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755>\
**Category:** Using Django\
**Created:** [August 4, 2020, 8:22am UTC](https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755 "2020-08-04T08:22:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![clarkeustaquio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/clarkeustaquio/32/6132_2.png) [@clarkeustaquio](https://forum.djangoproject.com/u/clarkeustaquio)\
**Post date:** [August 4, 2020, 8:22am UTC](https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755/1 "2020-08-04T08:22:56Z")

</div>

Is it possible to update the dictionary or list on my template using an ajax call?

Here is an example:

```
<div id="#loop">
<ul>
    {% for item in items %}
        <li>{{ item }}</li>
    {% endfor %}
<a href="backEndSomething/">Add</a>
</ul>
</div>

```

How can i update the variable ‘items’ without refreshing the page, if the variable is already updated in the back-end when i clicked the link (“Add”). So in my ajax call, i will not have to use appending new html syntax but instead to update the variable only and automatically update the html code aswell.

Because appending html files seems like a lot of work to do.

---

<div class="post-metadata">

**Author:** ![ephes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ephes/32/396_2.png) [@ephes](https://forum.djangoproject.com/u/ephes)\
**Post date:** [August 4, 2020, 10:09am UTC](https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755/2 "2020-08-04T10:09:28Z")

</div>

You can’t 🙂. The items variable lives on the server side. But you could fetch the list from `/api/items/`if the backend is already updated and then change your page based on the new data.

---

<div class="post-metadata">

**Author:** ![clarkeustaquio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/clarkeustaquio/32/6132_2.png) [@clarkeustaquio](https://forum.djangoproject.com/u/clarkeustaquio)\
**Post date:** [August 4, 2020, 11:45am UTC](https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755/3 "2020-08-04T11:45:28Z")

</div>

> [@clarkeustaquio](#):
>
> licked the link (“Add”).

So having a code like this is normal in ajax?

 ![ajax](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/7/7d6b7cd85753834e5193245ad4f0b95f2f169b54.png)

That is what i did just to apply ajax, its like creating another web page ☹

---

<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:** [August 4, 2020, 12:34pm UTC](https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755/4 "2020-08-04T12:34:19Z")

</div>

No, the typical way of doing this is to isolate that chunk of the template into its own file. You then create a view that renders just that chunk and returns _it_ as html back to the browser.

---

<div class="post-metadata">

**Author:** ![clarkeustaquio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/clarkeustaquio/32/6132_2.png) [@clarkeustaquio](https://forum.djangoproject.com/u/clarkeustaquio)\
**Post date:** [August 4, 2020, 12:42pm UTC](https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755/5 "2020-08-04T12:42:27Z")

</div>

Can you explain how is it done? Just a simple illustration of the concept. Thank you!

---

<div class="post-metadata">

**Author:** ![clarkeustaquio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/clarkeustaquio/32/6132_2.png) [@clarkeustaquio](https://forum.djangoproject.com/u/clarkeustaquio)\
**Post date:** [August 4, 2020, 12:43pm UTC](https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755/6 "2020-08-04T12:43:34Z")

</div>

How will i pass the data response coming from the success method in ajax to template file?

---

<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:** [August 4, 2020, 1:00pm UTC](https://forum.djangoproject.com/t/how-to-update-the-dictionary-in-django-templates-using-ajax/3755/7 "2020-08-04T13:00:56Z")

</div>

That’s not quite the idea.

You have some button that performs an action - starting the AJAX process.

That action results in some URL being invoked. **That** URL, instead of returning data as a JSON data structure, returns HTML instead.

Your success function then injects that HTML into the page.
