Worn out on such trifles, but I can not figure out how to output the following array)

There is such:

return render(request, 'main.html', {'arrImages': arrImages,'title': arrTitle}) 

The arrImages array is output without problems. But how to display the title ? I do not understand where you need a loop-it.

 <div class="container"> {% for images in arrImages %} <hr> <p class="text-justify"> {{ /////????title }}</p> <img class = "col-12 ml-auto col-12 mr-auto" src={{ images }}> {% endfor %} 

Where would I not set, does not work, or is it done in one cycle? Sorry for such questions, but I did not find in the internet)

views.py code

 def main(request): d3 = requests.get('https://d3.ru/api/posts/?sorting=rating&threshold_date=day').json() arrImages = [] arrTitle = [] arrText = [] try: for data in d3['posts']: imagesPath= ('data', 'link','url') images = reduce(lambda c, k: c[k] if c is not None and k in c else None, imagesPath, data) if images is not None and ".gif" in images and ".gifv" not in images: arrImages.append(images) imagesPath2= ('data', 'media','url') images2 = reduce(lambda c, k: c[k] if c is not None and k in c else None, imagesPath2, data) if images2 is not None: arrImages.append(images2) titlePath= ('data', 'title') textPath= ('data', 'text') title = reduce(lambda c, k: c[k] if c is not None and k in c else None, titlePath, data) text = reduce(lambda c, k: c[k] if c is not None and k in c else None, textPath, data) if title is not None: arrTitle.append(title) if text is not None: arrText.append(text) print('arrImages',arrImages) print('title',arrTitle) print('text',arrText) except BaseException as e: print('ecxeptions: ',e) return render(request, 'main.html', {'arrImages': arrImages,'title': arrTitle}) 
  • {% for item in title%} - not working? - Basalex
  • Doesn't work, but why are the records different? By analogy, should {% for title in arrTitle%} be like?) - Romik romikromik
  • Then it should be not for images in arrImages , but {% for i in range(len(arrImages)) %} , and then in the right places {{ arrImages[i] }} and {{ title[i] }} - Avernial
  • @Romik romikromik - no, the key {..., 'title' is used as the variable name: arrTitle}, as you see, the key is the title, you need to iterate the same way according to it - Basalex
  • “But why are the records different?” - so you yourself wrote different 'title': arrTitle in the code 'title': arrTitle - andreymal

1 answer 1

Here's what it looks like:

 return render(request, 'main.html', {'imageTitles': zip(arrImages, arrTitle)}) 

Then:

 <div class="container"> {% for image, title in imageTitles %} <hr> <p class="text-justify">{{ title }}</p> <img class = "col-12 ml-auto col-12 mr-auto" src={{ image}}> {% endfor %}