这是一个火车票预订系统,我试图在同一URL路径上传递2个id(trip_id / passenger_id),以便django可以记住乘客人数,然后根据乘客人数创建许多门票表格通过ID
path('trips/<int:trip_id>/',views.ticket_booking, name='trips'),
path('trips/<int:trip_id>/<int:passenger_id>/',views.passengers_page, name='ptrips'),
path('review/<int:ticket_id>/',views.review_page, name='rtickets'),
re_path(r'^from/(?P<start_station>[0-9]{1,50})/$', views.details_page, name='details_page'),
path('', views.details_page, name='booking'),
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.core.exceptions import ValidationError
from .models import Ticket, Trip, Passenger
from django.http import Http404
from django.shortcuts import get_object_or_404
def ticket_booking(request, trip_id, passenger_id):
trip = get_object_or_404( Trip, pk=trip_id)
passengers = list(Passenger.objects.filter(pk=passenger_id))
error = None
ticket = None
if request.method == 'POST':
first_name = request.POST.get('first_name')
middle_name = request.POST.get('middle_name')
last_name = request.POST.get('last_name')
email = request.POST.get('email')
gender = request.POST.get('gender')
ticket = Ticket(trip=trip,first_name=first_name, middle_name=middle_name,
last_name=last_name, email=email,gender=gender)
try:
ticket.full_clean()
ticket.save()
return redirect('tickets',ticket_id=ticket.id)
except ValidationError as e:
error = dict(e)
print(e)
context = { 'trip': trip,'error':error, 'ticket':ticket, 'passengers':passengers }
return render(request, 'details/ticket.html', context)
<div class="container marketing">
{% for trip in trips %}
<div class="card text-center ">
<div class="card-header">
Ride Details
</div>
<div class="card-body">
<h4>Start</h4>
<h5 value="{{trip.id}}">{{trip.start_station}}</h5>
<h4>End</h4>
<h5 value="{{trip.id}}">{{trip.end_station}}</h5>
<a class="btn btn-primary" href="{% url 'ptrips' trip_id passenger.id %}" role="button">Book Now</a>
</div>
</div>
{% endfor %}
</div>