How to render two views into one template using Django

Question:

fair to mention I’m just starting out with learning Django, and I could really use the help.

To paint a clear picture, there are two API endpoints that I have to use and each one of them has to be rendered in the same template (HTML file) meaning they have to be in the same URL path as well, but I couldn’t figure it out yet, I tried with both the function and class-based views but I got nowhere.

views.py:

class HomePage(TemplateView):
    template_name = 'home.html'

    def home(self, request):
        response = requests.get('https://api.covid19api.com/world/total').json()
        print('res',response.data)
        return render(request, 'home.html', {'response': response})


    def other(self, request):
        response= requests.get('https://api.covid19api.com/country/south-africa/status/confirmed?from=2020-09-06T00:00:00Z&to=2020-09-11T00:00:00Z').json()
        return render(request, 'home.html', {'response': response})

urls.py:

urlpatterns = [
    path('home', HomePage.as_view(), name='home')
]

home.html:

{% extends '_base.html' %}

{% block page_title %} Covid19 Statistics {% endblock %}


{% block content %}

<br>

<br>

<h3 class="text-center">World Total Statistics </h3>

<br>

<!-- <div class="container">
    <div class="card-group">


        <div class=" col-md-4">
            <div class="card text-white bg-secondary mb-3" style="max-width: 20rem;">
                <div class="card-body">
                    <h5 class="card-title text-center">Total Confirmed: {{response.TotalConfirmed}}</h5>
                </div>
            </div>
        </div>

        <div class=" col-md-4">
            <div class="card text-white bg-secondary mb-3" style="max-width: 20rem;">
                <div class="card-body">
                    <h5 class="card-title text-center">Total Deaths: {{response.TotalDeaths}}</h5>
                </div>
            </div>
        </div>

        <div class=" col-md-4">
            <div class="card text-white bg-secondary mb-3" style="max-width: 20rem;">
                <div class="card-body">
                    <h5 class="card-title text-center">Total Recovered: {{response.TotalRecovered}}</h5>
                </div>
            </div>
        </div>
    </div>
</div>
 -->

<br><br>

<h4 class="text-center">Get Statistics For a Specific Country </h4>

{% for object in response %}

<div class="card text-light bg-dark" style="width: 35rem;">
    <h3 class="card-header"></h3>
    <div class="card-body">
      <p class="card-text">{{object.Country}} </p>
      <p class="card-text">{{object.Date}} </p>
    </div>
  </div>

{% endfor %}

{% endblock%}

Any help is appreciated, thank you in advance

Asked By: Batool Ragayah

||

Answers:

You can call two api in one view as:

 def home(self, request):
    first_response = requests.get('https://api.covid19api.com/world/total').json()
    second_response = requests.get('https://api.covid19api.com/country/south-africa/status/confirmed?from=2020-09-06T00:00:00Z&to=2020-09-11T00:00:00Z').json()
   
    return render(request, 'home.html', {'first_response ': first_response ,'second_response':second_response })
Answered By: Manoj Kamble