I'm trying to pass the URL's of a list of photos to the template. I'm concatenating my MEDIA_URL to every photo name.
When print(a) is run, I can see in the console that the concatenation was successfully added. The result is something like media/photo.jpg. However by the time the loop finishes, the result reverts back to the original photo.jpg as if no concatenation happened. print(photos) shows a list of photos with no changes.
Why?
def get_property_data(request):
property_id = request.GET.get('id')
photos = ListingPhoto.objects.values_list('photo', flat=True).filter(listing__id=property_id)
for a in photos:
a = settings.MEDIA_URL + a
print(a)
print(photos)
return JsonResponse({'property': list(photos)})
for a in photos,ais copy of the elements insidephotos. Therefore modifyingadoesn't modify the values in the iterablephotos. Maybe you want:for i in range(len(photos)): photos[i] += settings.MEDIA_URL.