Cannot get API Json data to display on Django Template
I am trying to get data from a JSON, which was fetched from an API I created to display on my Django Template.
The API is just a simple API which store a post hit counts and its id, the json is formatted as below:
[
{
"object_pk": 3,
"hits": 15
},
{
"object_pk": 1,
"hits": 21
}
]
Here is my views.py file:
class BlogListView(ListView):
model = Post
template_name = "home.html"
class BlogDetailView(HitCountDetailView, DetailView):
model = Post
template_name = "post_detail.html"
count_hit = True
data = {
"hits": get_cloudapi_data(),
}
class ResumePageView(TemplateView):
template_name = "resume.html"
And my service.py file, which have the get_cloudapi_data() function:
def get_cloudapi_data():
url = "my-api-address"
r = requests.get(url)
hitcount = r.json()
return hitcount
Below is my HTML template, post_detail.html used for displaying this data:
{% extends "base.html" %}
{% load hitcount_tags %}
{% block content %}
<div class="post-entry">
<h2>{{ post.title }}</h2>
<p> {% get_hit_count for post %} views</p>
<p> {{ post.body }}</p>
{% for hit in hits %}
<p>{{ hit.object_pk }}</p>
<p>{{ hit.hits }}</p>
{% endfor %}
</div>
{% endblock content %}
It only shows the title, body and the hit count retrieve with the hitcount app not my API data
I have printed out the hitcount and the data if it would help
hitcount
[{'object_pk': 3, 'hits': 15}, {'object_pk': 1, 'hits': 21}]
data
{'hits': [{'object_pk': 3, 'hits': 15}, {'object_pk': 1, 'hits': 21}]}
I am new to working with an API with Django, so I am unsure where I went wrong.