Home > front end >  Trying to make API call, but keep getting TypeError
Trying to make API call, but keep getting TypeError

Time:11-11

I'm failing to make a simple API call in Django to search for available flights.

As per the API documentation, it accepts date_from and date_to parameters in 'MM/DD/YYYY' string format.

I'm trying to convert the input date format from the HTML form ('YYYY-MM-DD') to 'MM/DD/YYYY' using strptime and strftime but it doesn't seem to work.

I'm getting 'TypeError at /strptime() argument 1 must be str, not None'

What am I doing wrong?

home.html

<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Home</title>
  </head>
  <body>
    {{ response }}
    <form method="post">
      {% csrf_token %}
      <h3>Search Form</h3>
      <p>
        <input type="text" name="Origin" id="inputOrigin" placeholder="From" required>
      </p>

      <p>
        <input type="text" name="Destination" id="inputDestination" placeholder="To" required>
      </p>

      <p>
        <input type="date" name="Departuredate" id="idDeparturedate" required>
      </p>

      <p>
        <input type="number" name="Adults" id="idAdults" required>
      </p>

      <input type="Submit" name="Submit" value="Submit">
    </form>
  </body>
</html>

views.py

from django.shortcuts import render
import requests
import datetime as dt

# Create your views here.
def home(request):

    origin = request.POST.get('Origin')
    destination = request.POST.get('Destination')
    dep_date = request.POST.get('Departuredate')
    adults = request.POST.get('Adults')
    departure_date = dt.datetime.strptime(dep_date,'%Y-%m-%d').strftime('%d/%m/%Y')

    kwargs = {
                'apikey':'UkyTNeGok4791FIGnTeFMD6UrooUWXoI',
                'fly_from':origin,
                'fly_to':destination,
                'date_from':departure_date,
                'date_to':departure_date,
                'adults':adults
            }

    r = requests.get('https://tequila-api.kiwi.com/',params=kwargs).json()
    return render(request,'searchmyflight/home.html',{'response':r})

CodePudding user response:

When you first open the web page you are sending a GET request.

In this view you are trying to get the POST values even if the current method is GET. The POST values are none. So it gives you that error.

What you can do is use request.method to only do the part with POST values when the method is POST. See below.

from django.shortcuts import render
import requests
from django.conf import settings
import datetime as dt

def home(request):
    r = None
    if request.method == 'POST':
        origin = request.POST.get('Origin')
        destination = request.POST.get('Destination')
        dep_date = request.POST.get('Departuredate')
        adults = request.POST.get('Adults')
        departure_date = dt.datetime.strptime(dep_date, '%Y-%m-%d').strftime('%d/%m/%Y')

        kwargs = {
            'apikey': settings.API_KEY,
            'fly_from': origin,
            'fly_to': destination,
            'date_from': departure_date,
            'date_to': departure_date,
            'adults': adults
        }

        r = requests.get('https://tequila-api.kiwi.com/', params=kwargs).json()
    return render(request, 'searchmyflight/home.html', {'response': r})

Also just as a side note you may want to look at storing your API keys in the settings file.

  • Related