मैं एक परियोजना के हिस्से के रूप में एक एपीआई का उपयोग करने का प्रयास कर रहा हूं जिस पर मैं काम कर रहा हूं और मुझे वर्तमान में कुछ कठिनाई हो रही है:

एपीआई https://developers.amadeus. com/self-service/category/air/api-doc/airport-and-city-search

एपीआई - पायथन एसडीके https://github.com/amadeus4dev/amadeus-python

Django API गाइड https://developers.amadeus.com/blog/django- jquery-ajax-airport-search-autocomplete

मैंने उपरोक्त गाइड के निर्देशों का पालन किया है; हालांकि, मुझे कोड वाले इंडेक्स पेज को प्रस्तुत करने का प्रयास करते समय निम्न त्रुटि प्राप्त हो रही है:

Reverse for 'origin_airport_search' not found. 'origin_airport_search' is not a valid view function or pattern name.

इंडेक्स टेम्प्लेट:

{% extends "flightfinder/layout.html" %}

{% block body %}
<div id="mainhomepagediv">
    <div class="container-fluid px-0" id="mainhomepagediv">
        <div class="row mx-0">
            <div class="col-12 px-0">
                <img src="https://i.ibb.co/rHYzcyc/Landscape2.jpg" class="img-fluid w-100">
            </div>
        </div>
    </div>
    <div>
        <h1 id="homepageh1">Where are you flying to?</h1>
        <div id="homepagebox">
            <div>
                <form id="homepageform" method="POST">
                    <div class="row">
                      <div class="col">
                        <label id="homepageformlabel">From</label>
                        <input type="text" class="form-control" placeholder="Origin city" id="inputOrigin">
                      </div>
                      <div class="col pb-3">
                        <label id="homepageformlabel">To</label>
                        <input type="text" class="form-control" placeholder="Destination city">
                      </div>
                    </div>
                    <div class="row">
                        <div class="col">
                          <label id="homepageformlabel">Depart</label>
                          <input type="date" class="form-control" placeholder="Origin city">
                        </div>
                        <div class="col">
                            <label id="homepageformlabel">Return</label>
                            <input type="date" class="form-control" placeholder="Destination city">
                          </div>
                        <div class="col-6">
                          <label id="homepageformlabel">Class</label>
                          <select id="inputState" class="form-control">
                            <option selected>Economy</option>
                            <option>Business</option>
                            <option>First</option>
                          </select>
                        </div>
                      </div>
                      <div class="row">
                        <div class="col">
                        </div>
                        <div class="col-3 pt-3">
                            <button class="btn float-right" id="homepageformbutton" type="submit">Search flights</button>
                        </div>
                      </div>
                  </form>
            </div>
        </div>
    </div>
</div>    
<script>
  // Amadeus origin airport search
  $(document).ready(function () {
      $("#inputOrigin").autocomplete({
          source: "{% url 'origin_airport_search' %}",
          minLength: 1,
          delay: 200,
      });
  });
</script>
{% endblock %}

लेआउट टेम्प्लेट:

{% load static %}

<!doctype html>
<html lang="en">
  <head>
    <!-- jQuery & jQuery UI -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

    <!-- Bootstrap (includes Popper) -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Main CSS-->
    <link rel="stylesheet" type="text/css" href="{% static 'flightfinder/styles.css' %}">

    <!-- Main JS -->
    <script src="{% static 'flightfinder/main.js' %}"></script>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">

    <!-- Font Awesome Icons -->
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">

    <title>{% block title %}Flight Finder{% endblock %}</title>
  </head>
  <body>
      <nav class="navbar navbar-expand-lg navbar-light">
        <a class="navbar-brand" href="{% url 'flightfinder:index' %}">
            <img src="https://i.ibb.co/TRBkPBZ/Flight-Finder-removebg-preview.png" width="166" height="42" class="d-inline-block align-top" alt="">
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
          <div class="navbar-nav">
            <a class="nav-item nav-link active" href="{% url 'flightfinder:index' %}">Flights <span class="sr-only">(current)</span></a>
            <a class="nav-item nav-link" href="#">Hotels</a>
            <a class="nav-item nav-link" href="#">Car Hire</a>
          </div>
        </div>
      </nav>
    {% block body %}
    {% endblock %}
  </body>
</html>

मुख्य - urls.py:

"""finalproject URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('flightfinder.urls', namespace='Flightfinder')),
    path('admin/', admin.site.urls),
]

ऐप - urls.py:

from django.urls import path

from . import views

app_name = 'flightfinder'

urlpatterns = [
    path('', views.index, name='index'),
    path('origin_airport_search/', views.origin_airport_search, name='origin_airport_search')
]

views.py:

import json
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib import messages
from amadeus import Client, ResponseError, Location

from .models import Airport, Flight, Passenger

amadeus = Client(client_id='removed', 
                 client_secret='removed', 
                 log_level='debug') 

# Create your views here.
def index(request):
    return render(request, 'flightfinder/index.html')

def origin_airport_search(request):
    if request.is_ajax():
        try:
            data = amadeus.reference_data.locations.get(keyword=request.GET.get('term', None), subType=Location.ANY).data
        except ResponseError as error:
            messages.add_message(request, messages.ERROR, error)
    return HttpResponse(get_city_airport_list(data), 'application/json')

def get_city_airport_list(data):
    result = []
    for i, val in enumerate(data):
        result.append(data[i]['iataCode']+', '+data[i]['name'])
    result = list(dict.fromkeys(result))
    return json.dumps(result)

मैंने कुछ समान स्टैक प्रश्नों को देखा है, लेकिन उनमें से किसी ने भी अब तक मदद नहीं की है। आभारी होंगे अगर किसी के पास यहां कोशिश करने के लिए कोई विचार है?

शुक्रिया!

2
Tjm92 11 नवम्बर 2020, 16:09

1 उत्तर

सबसे बढ़िया उत्तर

आपने अपने शामिल में एक नाम स्थान "फ्लाइटफाइंडर" सेट किया है। इसका मतलब है कि आपको अपने यूआरएल को कॉल करने के लिए इस नेमस्पेस का इस्तेमाल करना होगा:

      source: "{% url 'Flightfinder:origin_airport_search' %}",
1
Antoine Pinsard 11 नवम्बर 2020, 16:34