Home > other >  Passing additional information to View
Passing additional information to View

Time:01-25

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?

CodePudding user response:

Best write your validations in the form methods. Read about in: https://docs.djangoproject.com/en/4.0/ref/forms/validation/#validating-fields-with-clean


from django.core.exceptions import ValidationError


class BookParkingSpotForm(forms.Form):
    # ...

    def clean(self):
       cleaned_data = super().clean()

       p_list = ParkingSpot.objects.all().exclude(
           parkingbooking__booking_time_start__lte=cleaned_data.booking_time_end,
           parkingbooking__booking_time_end__gte=cleaned_data.booking_time_start
        )
        if p_list.count() == 0:
            raise ValidationError('Your error message') 


And to access to the form data use form.cleaned_data['field_name'] and not form.instance.field_name.

CodePudding user response:

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})
  •  Tags:  
  • Related