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.

1 Answer 1

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 }}); 
Sign up to request clarification or add additional context in comments.

4 Comments

Hello Thank you for your answer
Please could you explain me the $(newdoc).filter line
var secondList = $(newdoc).filter(".secondList").add($(newdoc)).find(".secondList)); TOKEN ILLEGAL Could you help me please
@user1987916 I accidentally introduced an error. See edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.