How to render results from API based on the user's search using Django

As you can see in the picture below I'm trying to have the user search for a given country, start/end date and get the result of "Confirmed Cases" and "Date" back from the API, but I'm not sure how to do it.

Image

I tried using this API, to fill the drop-down menu of the countries -->

https://api.covid19api.com/summary

but this is the other API that I have to use but while changing the parameters for the country and dates -->

https://api.covid19api.com/country/afghanistan/status/confirmed?from=2020-09-06T00:00:00Z&to=2020-09-11T00:00:00Z

Here are snippets of my code:

views.py

def home(request):
   # second_response = requests.get('https://api.covid19api.com/country/afghanistan/status/confirmed?from=2020-09-06T00:00:00Z&to=2020-09-11T00:00:00Z').json()
    second_response = requests.get('https://api.covid19api.com/summary').json()

    my_list = []
    for i in range(0, len(second_response['Countries'])):
        my_list.append(second_response['Countries'][i]['Country'])

    if request.method=='POST':
        selected_country = request.POST['selected_country']
        print('here', selected_country)
    
    return render(request, 'home.html', {'my_list': my_list})

home.html

<div class="container justify-content-center">
  <form action="{% url 'home' %}" method="post">
    {% csrf_token %}
    <label for="selected_country" style="margin-right: 5px;"> Select a Country, Start & End Dates : </label>
    <select name="selected_country" >
      {% for object in my_list %}
      <option value="">{{object}}</option>
      {% endfor %}
    </select>
    <label for="startdate"></label>
    <input type="date" id="startdate">

    <label for="enddate"></label>
    <input type="date" id="enddate">

    <input type="submit" value="Search" />
  </form>
</div>

PLUS: when I click on "search" i should get the value of the selected_country because I tried printing it, but it doesn't show for some reason, so the method is post but for some reason I can't get back the selected_country

Any help is appreciated

JAVASCRIPT

if you have any solid grasp of javascript i recommend you do that in javascript, because it will just make it better and easier

otherwise :

view.py


def handler(request):

    if request.method=='POST':
        selected_country = request.POST['selected_country']
        startDate= request.POST['startdate']
        endDate= request.POST['enddate']
        
    request_handler = requests.get(f"https://api.covid19api.com/country/{selected_country}/status/confirmed?from={startDate}T00:00:00Z&to={endDate}T00:00:00Z")
    if request_handler.status_code=200:
        #To prevent errors
        request_json=request_handler.json()
    else:
        pass # do something



    
    return render(request, 'result.html', {"json":request_json})
    #you should handle the data at the front end using jinja blocks

note : i don't know much about Django so the code may break

Back to Top