1

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 %}

1 Answer 1

1

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 %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.