AttributeError:模块“ helpers”没有属性“ flights”

时间:2020-09-07 07:53:47

标签: python json flask

我一直试图调用名为flights的列表,该列表位于主应用程序文件的帮助文件中。我已经尝试过使用import helpersfrom helpers import *from helpers import flights,但是它仍然给我同样的错误。我也尝试过将列表称为helpers.flights,但这也给了我这个错误。我可以成功地从助手中导入函数,而不仅仅是此列表。

这是列表所在的功能:

def lookup(flight):
    """Look up information for a flight."""

    global flights
    flights = []

    # Contact API
    api_key = os.environ.get("API_KEY")

    params = {
      'access_key': api_key,
      'airline_name': request.form['airline'],
      'flight_number': request.form['flight_number'],
      'limit': '1'
    }

    api_result = requests.get('http://api.aviationstack.com/v1/flights', params=params)

    api_response = api_result.json()


    for flight in api_response['data']:
        if flight.get('live') and not flight['live']['is_ground']:
            print('{} flight {} from {} ({}) to {} ({}) is in the air.'.format(
                flight['airline']['name'],
                flight['flight']['iata'],
                flight['departure']['airport'],
                flight['departure']['iata'],
                flight['arrival']['airport'],
                flight['arrival']['iata']))
            flights.append(flight['airline']['name'])
            flights.extend(flight['flight']['iata'],
                           flight['departure']['airport'],
                           flight['departure']['iata'],
                           flight['arrival']['airport'],
                           flight['arrival']['iata'])
            print(flights)
        else:
            print('{} flight {} from {} ({}) to {} ({}) has landed.'.format(
                flight['airline']['name'],
                flight['flight']['iata'],
                flight['departure']['airport'],
                flight['departure']['iata'],
                flight['arrival']['airport'],
                flight['arrival']['iata']))
            flights.append(flight['airline']['name'])
            flights.extend(flight['flight']['iata'],
                           flight['departure']['airport'],
                           flight['departure']['iata'],
                           flight['arrival']['airport'],
                           flight['arrival']['iata'])
            print(flights)

我的应用程序文件看起来像这样:

import os

from cs50 import SQL
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
from werkzeug.security import check_password_hash, generate_password_hash

import helpers
from helpers import apology, login_required, lookup

@app.route("/search", methods=["GET", "POST"])
@login_required
def search():
    """Get flight information."""

    # User reached route via POST
    if request.method == "POST":

        flights = helpers.flights

        return render_template("results.html", search=search, flights=flights)

    # User reached route via GET
    else:
        return render_template("search.html")

我觉得这是一个简单的修复程序,但我尝试过的任何方法都无法使其正常工作。

0 个答案:

没有答案