I am learning the Django framework. Today I was trying to retrieve a URL in HTML template by using
<form action="{% url 'book' flight.id %}" method="post">
Also, I've got a function book. Here is the code:
def book(request,flight_id):
if request.method=="POST":
flight=Flight.objects.get(pk=flight_id)
passenger=Passenger.objects.get(pk=int(request.POST["passenger"]))
passenger.flights.add(flight)
return HttpResponseRedirect(reverse("flight",args=(flight.id,)))
And this is the urls.py file :
urlpatterns = [
path("",views.index,name="index"),
path("<int:flight_id>",views.flight,name="flight"),
path("<int:flight_id/book",views.book,name="book")
]
But I don't know why it is showing an error:
NoReverseMatch at /1 Reverse for 'book' with arguments '(1,)' not found. 1 pattern(s) tried: ['<int:flight_id/book\Z']
CodePudding user response:
Your book url pattern is not correct, there is a minor error. You are missing a clossing >
here path("<int:flight_id/book",views.book,name="book")
. It should look like this
urlpatterns = [
path("",views.index,name="index"),
path("<int:flight_id>",views.flight,name="flight"),
path("<int:flight_id>/book",views.book,name="book")
]