找不到带有关键字参数'{'pk':2}'的'plane_detail_book'反向1个尝试的模式:['<int:\\ pk =“”> / $']

时间:2019-03-15 04:36:49

标签: django python-2.7 django-forms django-templates django-views

我正在尝试从某个特定的来源到目的地进行航空公司搜索,并在列出飞机之后我希望这些飞机成为链接,以便单击它们将我带到飞机详细信息页面,但由于某些原因该错误不断弹出。我对此并不陌生,因此似乎无法找出问题所在。任何帮助将不胜感激。

  

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'),
]
  

模板

<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 个答案:

答案 0 :(得分:0)

从此更改视图

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

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

还要更改模型中的绝对网址

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

更新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>

删除空间

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

答案 1 :(得分:0)

我认为您需要更改详细信息视图:

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

收件人

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})