[Fixed]-How do you update a django template context variable after an AJAX call?

31๐Ÿ‘

โœ…

you cannot this way. Better way would be to load that part of your html via ajax.

your ajax view:

def update_items(request):
    product_list = your_data
    return render(request, 'table_body.html', {'product_list':product_list})

your main html:

<tbody class="table_body">
   {% include 'table_body.html' %}
</tbody>

table_body.html:

{% for item in product_list %}
  <tr>
     <td colspan="2">{{ item.date }}</td>
     <td id="item_name_format" colspan="6">{{ item.name }}</td>
     {% if item.category_id %}
      <td id="item_name_format" colspan="2">{{ item.category_id.level1_desc }}</td>
     {% endif %}
      <td id="item_amt_format" colspan="2">${{ item.amount|intcomma }}</td>
  </tr>
{% endfor %}

your ajax would look like this:

function update_item(item_num) {
    console.log(item_num) // sanity check
    $('.table_body').html('').load(
        "{% url 'update_items' %}?item_num=" + item_num
    ); // <--- this code instead of $.ajax(lala)

you use this load() here

๐Ÿ‘คdoniyor

-1๐Ÿ‘

you need first to create model for your product items

then in your views.py home function, load items from database and passing it to context

example:

myProduct = Products.objects.all()
context = {'myList': myProduct}
return render(request, "home.html", context)

then add this java code to your templete and call it if user want to add item or remove it from card list

like:

onClick="toggleCardItem({{item.id}})"

then

function toggleCardItem(itemId){
     $.ajax({
        type: 'POST',
        url: 'client/add/card',
        data: {'id': itemId},
        success: function(res){
             location.reload();
        }
     })
}

python templete:

{% for item in myList %} etc.....

now on your views.py create

@csrf_exempt
def add_to_card(request):
   if request.method == "POST":
       item_id = request.POST.get("id")
       key = request.session.session_key
       if key:
           find = Clients.objects.filter(session_key=key)
           if find.exists():
               find = find.get()
               card = find.card_list if find.card_list else "[]"
               card = eval(card)
               item_id = int(item_id)
               if item_id in card:
                   card.remove(item_id)
               else:
                   card.append(item_id)
               find.card_list = card
               find.save()
               return HttpResponse("Success")
           else:
               raise PermissionDenied("This page is private, you not allowed to entered!!!")
       else:
           raise PermissionDenied("This page is private, you not allowed to entered!!!")
   else:
       raise PermissionDenied("This page is private, you not allowed to entered!!!")

import python library:

from django.views.decorators.csrf import csrf_protect, csrf_exempt

now in url.py add

path("client/add/card", views.add_to_card, name="Add to Card"),
๐Ÿ‘คOmar Othman

Leave a comment