I have this html code which takes a list of images (of any reasonable length) and displays them in a table with 4 columns
<div>
<table>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
{% for image in image_list %}
{% cycle "<tr>" '' '' '' %}
<td align="center"><img src="{{ image }}"></td>
{% cycle '' '' '' "</tr>" %}
{% endfor %}
</table>
</div>
I am converting the page to work asynchronously using Ajax. I have the list of images in a javascript array, but I do not know how to send image_list to the html from javascript. This is my javascript so far
function displayImageList(message_text, context) {
console.log(context);
}
function bid_made(username) {
var object = ''
$.ajax(
{
type:"GET",
url: "get-image-list/"+username,
cache: false,
data:{object: object},
success: function(context) {
displayImageList(status, context)
}
})
}
views.py
def get_image_list(request, username):
if request.method == 'GET':
user_images = app.get_user_images(username)
context = {'user_images': user_images}
return JsonResponse(context, safe=False)
else:
return HttpResponse('unsuccessful')
My console log shows that context contains an Array, so I am happy it is working that far. The console log displays:
{user_images: Array(3)}
I need to extend the displayImageList function.
I can update a single item on the page using a function
function showImage(src, width, height, alt) {
document.getElementById("top-image").src = src;
};
But updating from a list defeats me. Can someone show me how to do it?
get-image-list/"+username