DEV Community

Vicente G. Reyes
Vicente G. Reyes

Posted on

[SOLVED]Show data from API on Django tempalte

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?

2

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,

Top comments (7)

Collapse
 
sepyrt profile image
Dimitris R • Edited

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 %}
Collapse
 
highcenburg profile image
Vicente G. Reyes

HA! Thank you!

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

Collapse
 
sepyrt profile image
Dimitris R

No problem, glad to help.

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

Collapse
 
highcenburg profile image
Vicente G. Reyes

Do you want to answer the question on SO?

Collapse
 
xarala221 profile image
Ousseynou Diop

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

Collapse
 
xarala221 profile image
Ousseynou Diop

SOLVED

Collapse
 
highcenburg profile image
Vicente G. Reyes

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.