0

I would like to update a template with Ajax.

My problem is:

  • I select a client in a list on the form and display only the corresponding data in an another list on the same page in a second list
  • At this time, I can not update my template with protocols corresponding to the client

  • In my views, I try to create a list with a queryset (it works) but I cannot update my template with the new list

  • I retrieve the selected client but when I post with render_to_request it does not update the template
  • Is there any possibility to do that and how can I update my list with the ajax part of the program.
Jed Fox
  • 2,979
  • 5
  • 28
  • 38

1 Answers1

0

You can use something like this:

(based on https://stackoverflow.com/a/21762215/5244995)

second_list.tmpl (template)

{% for value in corresponding_data %}
    <li>{{ value }} (replace with your own templating)</li>
{% endfor %}

views.py

def update_second_list(request, ob_id):
    # (get the data here)
    return render('second_list.tmpl', {'corresponding_data': ...}

JS script on main page (uses jQuery)

$.ajax({url:"", dataType:"text", success: function(html) {
    var newDoc = $.parseHTML(html, document, false); // false to prevent scripts from being parsed.
    var secondList = $(newDoc).filter(".secondList").add($(newDoc).find(".secondList"));
    $(".secondList").replaceWith(secondList); // only replace second list.
    // other processing
}});
Community
  • 1
  • 1
Jed Fox
  • 2,979
  • 5
  • 28
  • 38