'Passing additional information to View

I am trying to handle Django form in a more gentle way and actually, I have no idea how to push further this topic. I have a View which is responsible for displaying a Form, in If form.is_valid() condition I am looking for free parking spots, if I found nothing then I would like to show an Error to the user or pass to context additional data. Fist part I've covered easily but I have no idea how to show an error or something similar.

class BookParkingSpot(FormMixin, generic.ListView):
    template_name = 'parking/book_spot.html'
    form_class = BookParkingSpotForm
    model = ParkingBooking


    def post(self, request, *args, **kwargs):
        form = BookParkingSpotForm(self.request.POST)

        if form.is_valid():
            parking_spot_list = ParkingSpot.objects.all().exclude(
                parkingbooking__booking_time_start__lte=form.instance.booking_time_end,
                parkingbooking__booking_time_end__gte=form.instance.booking_time_start
            )

            if parking_spot_list.exists():
                random_spot = random.choice(parking_spot_list)
                reservation = ParkingBooking.objects.create(
                    car=form.instance.car,
                    booking_owner=self.request.user,
                    spot=random_spot,
                    booking_time_end=form.instance.booking_time_start,
                    booking_time_start=form.instance.booking_time_end,
                )
                return redirect(reservation.get_absolute_url())
            else: # this part doesn't work
                context = self.get_context_data(**kwargs)
                return render(self.request, self.template_name, context)

Any idea how to improve it?



Solution 1:[1]

Sorry everyone for the inconvenient but again after posting on StackOverflow I figured out how to resolve this issue and it was very simple, I am still learning Django ;)

So the answer to my question lies in the last part of my code, if I didn't find any free parking spots then I can render the same page with additional data like on below example and pass form object to context:

return render(self.request, 'parking/book_spot.html',
                          {'errors': "There are no free parking spots in selected time",
                           'form': form})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SerSergious