I need to get the value of the field that we enter in the form and immediately take action on it

I need to get the value of the field that we enter in the form (in this case, these are the days of reservation) and immediately calculate the cost of the reservation based on it And the problem is that I don’t understand how to get the value of these very fields (so this is not QuerySet request, and not accessing the database)

This is my views:

def booking(request):

error = ''

if request.method == 'POST':
    form = BookingForm(request.POST)
    if form.is_valid():
        booking = form.save(commit=False)
        booking.user = request.user
        booking.sum = #create sum function
        form.save()
        return redirect('account')
    else:
        error = 'Форма не корректна'

form = BookingForm()

context = {
    'form': form,
    'error': error
}
return render(request, 'bookings/booking.html', context)

And this is models:

class Booking(models.Model):

startdate = models.DateField('Startgdate')
finishdate = models.DateField('Finishdate')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
sum = models.PositiveIntegerField('Sum')
fullname = models.CharField('Full name', max_length=50)
pnumber = models.PositiveBigIntegerField('Phone number')

def __str__(self):
    return self.fullname

class Meta:
    verbose_name = 'Booking'
    verbose_name_plural = 'Bookings'

thanks in advance

This is needs Ajax (which can be done by JQuery or Fetch API) that runs on input onchange and a simple view that receives that value and return a the value of cost reservation.

Back to Top