[Python] Django passing values ​from views to html

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have two pages for reserving a table in a restaurant, and their logic is as follows: on the first page, the user enters their contact details, reservation date, and number of guests, and then clicks the "Next" button. They are then redirected to the second page, where they need to select a table and time (the available time slots are generated based on the selected date). The problem is that I can't pass the time values to the template of the second page through the view. Here's a rough example of the code:

    def contact_details(request):
    if request.method == 'POST':
    # Get the form data from the request
    # ...

    # Store the form data in the session
    request.session['client_name'] = client_name
    request.session['client_email'] = client_email
    request.session['client_phone'] = client_phone
    request.session['number_of_guests'] = number_of_guests
    request.session['reservation_date'] = reservation_date
    request.session['reservation_time'] = [str(time) for time in free_time()]

    return redirect('reservation_data')

    return render(request, 'contact_details.html')

    def reservation_data(request):
    if request.method == 'POST':
    # Get the form data from the session
    # ...

    # Create the reservation using the form data
    # ...

    # Clear the session data
    request.session.clear()

    return redirect('reservation_detail', pk=reservation.pk)

    return render(request, 'reservation_data.html')



    I understand that values are typically passed to the template using render, but if I use render in the first view, the second view won't be triggered to save the form values. So, I'm using redirect. Combining the two views into one is not possible because I need to process the date and generate the available time slots.

    Continue reading...

Compartilhe esta Página