[Fixed]-Django. Render model parameters by js

1๐Ÿ‘

โœ…

you should use .load() to load just part of the DOM, which should be used to show the newly fetched data from server.

the idea is:

<div>
  {% for object in object_list %}
     <a class="obj_cls" data-object_id="{{ object.id }}"></a>
  {% endfor %}
</div>
<div class="snippet_wrapper">
  {% include "html_snippet_only_for_this_part.html" %}
</div>

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
 $(function(){
  $('.obj_cls').on('click', function () {
    var object_id = $(this).data('object_id');
    $('.snippet_wrapper').html('').load("/"+object_id, function () {        
    });
  });
 });
</script>

and the view with url /PK/ where object_id is the ID of clicked object should do this:

def PartyDetailView(DetailView):
    model = YourModel
    template_name = "html_snippet_only_for_this_part.html"

and the content of html_snippet_only_for_this_part.html is just:

<div>
  {{ object.name }} etc etc... 
</div>

hope, this helps! ๐Ÿ™‚

๐Ÿ‘คdoniyor

Leave a comment