I am trying to make airline search from a certain source to destination and after listing the planes i want the planes to be links so that clicking on them takes me to the plane detail page but some reason this error keeps popping. I am new to this so cant seem to figure out the problem. Any help will be appreciated.

views.py

def index(request):
    form = SearchForm()

    if request.method == "POST":
        form = SearchForm(request.POST)

        if form.is_valid():
            return plane_list(request)
        else:
            print("ERROR")

    return render(request,'index.html',{'form':form})

def plane_list(request):

    form = SearchForm(request.POST or None)

    if request.method == "POST":
        form = SearchForm(request.POST)
        if form.is_valid():

            p = Route.objects.filter(route_dest = form.cleaned_data.get('destination'),route_src = form.cleaned_data.get('source'))
            if not p:
                route_id = 1000
            else:
                route_id = p[0].route_no
            flights = FlightDetail.objects.filter(route=route_id)

    return render(request, 'plane_list.html', {'form': form,'flights': flights})

def plane_detail_book(request):
    flights = FlightDetail.objects.all()
    return render(request, 'flightdetail.html',{'flights': flights})

models.py

class Route(models.Model):
    route_no = models.CharField(max_length = 10)
    route_dest = models.CharField(max_length = 100)
    route_src = models.CharField(max_length = 100)

class FlightDetail(models.Model):
    flight_no = models.CharField(max_length = 100,)
    route = models.CharField(max_length = 100)

    def get_absolute_url(self):
        return reverse("flight_detail_book",kwargs={'pk': self.pk})

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.index,name='index'),
    path('planes/',views.plane_list,name='plane_list'),
    path('<int: pk>/',views.plane_detail_book,name='plane_detail_book'),
]

template

<body>
    {% if flights %}
      {% for flights in flights %}
        <div class="container">     
          <a href="{% url 'plane_detail_book' pk=flights.pk %}">{{ flights.flight_no }}</a>
          </div>
      {% endfor %}
    {% else %}
      <h1>NO PLANES</h1>
    {% endif %}
  </body>
</html>

2 Answers

0
Exprator On Best Solutions

change the view from this

def plane_detail_book(request):
    flights = FlightDetail.objects.all()
    return render(request, 'flightdetail.html',{'flights': flights})

to

def plane_detail_book(request,pk):
    flights = FlightDetail.objects.all()
    return render(request, 'flightdetail.html',{'flights': flights})

also change the absolute url in your model

def get_absolute_url(self):
        return reverse("plane_detail_book",kwargs={'pk': self.pk})

update the html

<body>
    {% if flights %}
      {% for flight in flights %}
        <div class="container">     
          <a href="{% url 'plane_detail_book' flight.pk %}">{{ flight.flight_no }}</a>
          </div>
      {% endfor %}
    {% else %}
      <h1>NO PLANES</h1>
    {% endif %}
  </body>
</html>

remove the space

path('<int:pk>/',views.plane_detail_book,name='plane_detail_book'),
0
Niladry Kar On

I think you need to change you detail view:

def plane_detail_book(request):
    flights = FlightDetail.objects.all()
    return render(request, 'flightdetail.html',{'flights': flights})

To

from django.shortcuts import get_object_or_404

def plane_detail_book(request,pk):
    flights = get_object_or_404(FlightDetail, pk=pk)
    return render(request, 'flightdetail.html',{'flights': flights})