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.


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

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

Here are snippets of my code:

def home(request):
   # second_response = requests.get('').json()
    second_response = requests.get('').json()

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

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


<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 %}
    <label for="startdate"></label>
    <input type="date" id="startdate">

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

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

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


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 :

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"{selected_country}/status/confirmed?from={startDate}T00:00:00Z&to={endDate}T00:00:00Z")
    if request_handler.status_code=200:
        #To prevent errors
        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