loading...

[SOLVED]Show data from API on Django tempalte

highcenburg profile image Vicente G. Reyes ・1 min read

This is the continuation of my question yesterday and I'm now trying to show the fetched data on the django template.

Can anyone help me?

Show data from API on Django tempalte

0

I'm trying to show the fetched data on an API to the Django template.

Here's what I've tried on home.html

<h1>Title: {{data.title}}</h1>

Here's my views.py which gets the data from services.py

class IndexData(TemplateView):
    def get(self, request):
        article_data = services.get_data()
        return render(request, 'pages/home.html', article_data)

Here's the services.py

def get_data(title, url, description,

Discussion

markdown guide
 

Hello Vicente,

It appears that the data passed to the template is a list, so you would need a loop to access the title for each item.

{% for item in data %}
<h1>Title: {{ item.title }}</h1>
{% endfor %}
 

HA! Thank you!

You're right, I forgot to loop through the data.

 

No problem, glad to help.

I don't have a SO account though to answer the question there.

 

Do you want to answer the question on SO?

 

Hello,
You can use the requests Library to make it .
First of all, you need to serialize the data and send them to your template.
It's late night, tomorrow i will share with you a live example.
Thanks

 
 

Yep. Turns out I forgot to loop through the data on the template. But I'l still study DRF to learn how to serialize data. Thanks.