Python, django: I would like to put objects within the 'input' tag or 'select' tag on form. ..in order to transfer the objects to the booking_form

Viewed 41
  1. User put some information in the price inquiry form and hit the calculate button
  2. User is moved to the page displaying the price with the information user put in the form
  3. If user is happy with the price and wanna go ahead booking, then there is a booking button
  4. Once user hit the booking button, user would be accessed to the booking_form automatically
  5. On the booking form, User can see the information he put before, so he does not have to put them in again and he can complete the rest columns in the booking form.

-- this is what I wanted

So far, I did successfully upto no 4. I would like to transfer the information user put and the price calculated into the booking_form. but input tag does not allow {{ object }} inside tag.

To make you understood better, I have attached the 'price_detail.html' below.

{% extends 'basecamp/base.html' %}
{% block content %}
    <form action="{% url 'booking_form' %}" method="post">
        {% csrf_token %}
        <div class="container">
            <div class="py-md-6">
                <h1> The price :  $ {{ price }} </h1>
                
                <p class="fs-lg text-light" >{{ flight_date }} | {{ flight_number }} | {{ suburb }} | {{ no_of_passenger }} </p>
                <br/>
                
                <button class="btn btn-warning" type="submit">Book now</button>
            </div>
        </div>
{% endblock %}

I think I need to put the objects that I got from the price inquiry form into the input tag in order to transfer them into the booking_form but input tag/select tag does not allow that {{ }} inside tags.

Is there any alternative ways to transfer those objects into the booking form when User hit the booking button?

1 Answers

The easiest way is to put the item in as session and then in the form get the initial from session, e.g if the user selected a hotel then add a key booking_hotel_name by

  request.session["booking_hotel_name"] = selected_hotel

then at the view which loads the form, collect theses values in initials dict as then to the form while doing construction

Edit: Here is some explanation

According to django form documentation, the form has a initials dictionary which can be passed to set the initial value of the form fields, my idea to load this initial values from the session. so lets assume that your Form is called `ConfirmBokking' and it has a field called 'hotel_name' like below

class ConfirmBooking(forms.Form):
    hotel_name = forms.CharField()

As shown before, you added this to booking_hotel_name under the session, now when you are going to initialize the form in the, we do the following

  if request.method == "GET":
      initials = {"hotel_name":request.session.get("booking_hotel_name")}
      form = ConfirmBooking(initials = initials)

I hope it is clear now.

Related