Почему assertContains не работает должным образом в тестах django?

Я написал тест, который должен создать рейс, затем передать соответствующие аргументы в функцию client.get и получить ответ, который будет содержать результаты "поиска" - в данном случае это должен быть только рейс, созданный ранее как прямой рейс из Airport в AirportSeceond(имена или airport и airport2). Однако, похоже, что в ответе нет результатов (первое утверждение работает правильно). Мой тест (TestCase):

     def test_no_errors_html_direct_flights(self):
            country, city, airport, city2, airport2, airport3 = create_set()
            flight = SingleFlight.objects.create(departure_airport=airport, arrival_airport=airport2,
                                        departure_time_utc=(tz.now()+datetime.timedelta(days=5)),
                                        duration_in_hours=2, duration_in_minutes=10, 

price=200)
        print("\n\n\n")
        print(flight)
        date = (tz.now() + datetime.timedelta(days=5)).date().strftime("%Y-%m-%d")
        print(date)
        kwargs = {'dep': 'Airport', 'arr': 'AirportSecond', 'date': date}
        response = self.client.get(reverse('app1:flight-search-result'), kwargs)
        self.assertContains(response, 'No options for your search')
        self.assertContains(response, 'Direct connection')

Мой шаблон:

{% block content2 %}
{% if not direct_flights %}
{% if not indirect_flights %}
<p><strong>No options for your search</strong></p>
{% endif %}
{% endif %}
{% for flight in direct_flights %}
<p>{{ flight.departure_airport }}</p>
<p>{{ flight.departure_time_local }}</p>
<p>      </p>
<p>{{ flight.arrival_airport }}</p>
<p>{{ flight.arrival_time_local }}</p>
<p>Direct connection</p>                                      <---- this should be in the response
<a href="{% url 'app1:flight-search-detail-passenger-form' flight.id_flight %}">Here! ;)</a>
{% endfor %}

{% for connection in indirect_flights %}
{{ connection.price }}
{% for flight in connection.all_info %}
<p>{{flight.departure_airport }}</p>
<p>{{flight.arrival_airport}}</p>
<p>         </p>
{% endfor %}
<p>One change</p>
<a href="{% url 'app1:flight-search-detail-passenger-form' connection.first_id connection.second_id %}">Here! ;)</a>
{% endfor %}
{% endblock %}

Мои взгляды:

def flight_search_result(request):
    form = FlightSearchForm(request.GET)
    if form.errors:
        return render(request,'app1/index.html', {'form': form})
    else:
        date = request.GET.get("date")
        # if datetime.datetime.strptime(date, '%Y-%m-%d').date() <= datetime.date.today():
        #     return HttpResponseNotFound -- not necessary, the data get validated in the form anyway
        dep = request.GET.get("dep")
        arr = request.GET.get("arr")
        direct_flights = search_for_direct_flights(dep, arr, date)
        indirect_flights = search_for_indirect_flights(dep, arr, date)
        return render(request, 'app1/flight-search-result.html',
                      {'form': form, 'direct_flights': direct_flights,
                       'indirect_flights': indirect_flights})

Функция search_for_direct_flights:

def search_for_direct_flights(dep, arr, date):
    params = {'dep': dep, 'arr': arr, 'date': date}
    source = reverse('app1:api-flights')
    direct_flights = requests.get('http://127.0.0.1:8000' + source, params=params)
    return direct_flights.json()

Когда я использую веб-сайт, он работает правильно. Проблемы возникают только во время тестирования. Что здесь не так? Может ли это быть из-за этого вызова API? Спасибо за помощь.

Вернуться на верх