I have a m_template.html file which has a script as follow:
<script>
$("#id_country").change(function () {
var countryId = $(this).val(); // get the selected country ID from the HTML input
$.ajax({ // initialize an AJAX request
url: '/ajax/ajax_load_cities',
data: {
'countries': countryId // add the country id to the GET parameters
},
dataType: 'json',
success: function (data) { // here data comes from url output or load_cities function
$("#preview-items").html(data.tags);
}
});
});
</script>
In the same template.html I defined a section like:
<div id="preview-items">
{% for item in itemslist %}
<label class="btn btn-primary mr-1">
<input type="checkbox" id="checklist" value="{{ item.0 }}">
<span>
{{ item.1 }}
</span>
</label>
{% endfor %}
</div>
and load_cieties:
def load_cities(request):
....
data = {
'tags': list(cities)
}
return JsonResponse(data)
Where problem happens:
The problem is in the line $("#preview-items").html(data.tags); which rather than passing list, replaces list in the <div> so at the moment I have something like:
id1name1,id2name2,id3name3,...
My Question:
How to pass data.tags which is a list of tuples to section as a parameter that within which I be able to style list items as I already defined in ?
data.tags is something like [(id1, name1), (id2, name2), (id3, name3), ..]