Reputation: 29
Here I got the right URLs
from django.http import HttpResponse
from django.shortcuts import render
import requests
def main(request):
return render(request,'main.html')
req = requests.get('https://d3.ru/api/posts').json()
arr = []
for data in req['posts']:
urls = data['main_image_url']
if urls != None:
arr.append(urls)
print('urls',arr)
How can i pass array arr
into my template main.html
into <img class>
{% extends "wrapper.html" %}
{% block title %}
<div class="container">
<img class = "col-12 ml-auto col-12 mr-auto" src=///arr???>
</div>
{% endblock %}
Upvotes: 1
Views: 79
Reputation: 82765
You can pass your list as an argument in render
ant then loop over it in the template
Ex:
Views.py
def main(request):
req = requests.get('https://d3.ru/api/posts').json()
arr = []
for data in req['posts']:
urls = data['main_image_url']
if urls != None:
arr.append(urls)
print('urls', arr)
return render(request, 'main.html', {'arr': arr})
And Template
{% extends "wrapper.html" %}
{% block title %}
<div class="container">
{% for i in arr %}
<img class = "col-12 ml-auto col-12 mr-auto" src={{ i }}>
{% endfor %}
</div>
{% endblock %}
Upvotes: 1