I keep getting form.is_valid return false

I keep get form.is valid return false I guess the issue is coming any time I decide to use ModelChoiceField or ChoiceField in my Django form

Here is my forms.py

class bookbusform(forms.Form) :
  current_location =  forms.ChoiceField(choices = Bus_Terminal.objects.values_list("id","name"),widget = forms.Select(attrs={"class":"select"})
  Destination = forms.ChoiceField(choices = Bus_Terminal.objects.none(),widget = forms.Select(attrs={"class":"select","id":"Des"}))
  departure_date = forms.DateField(widget = forms.DateInput(attrs={"type":"date","min":min_date,"max":max_date}))

And here is my views.py

def book_bus(request):
  if request.method == 'POST' :
    form = bookbusform(request.POST)
    if form.is_valid() :
      cd = form.cleaned_data
      current_loc = cd['current_location']
      destination = cd['Destination']
      departure = cd['departure_date']                                      User = get_user_model()                                               if 'booknow' in request.POST  :
        user = User.objects.get(phone_number=request.user.phone_numbe>
        booking = Booking.objects.create(current_Location = current_l>
        booking.created_by  = user
        booking.save()                                                        return redirect('payonline')
      else :
        user = User.objects.get(phone_number=request.user.phone_numbe>
        booking = Booking.objects.create(current_Location = current_l
        booking.created_by  = user
        booking.save()
        return redirect('home')
                                                                          else :
      return redirect('error-page')

  else :
    form = bookbusform()
    context = {                                                             'form' : form
      }
    return render(request,'bookbus.html',context)

Any time I enter into the form and send I get views didn’t return anything but any time I change the form field to Charfield every thing works perfectly

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of modifying your original post to fix this.)

Your view wasn’t copied cleanly into your post. Please correct the code so that it can be evaluated.

Second, you shouldn’t be initializing your choice fields this way. At a minimum, choices should be a reference to a callable, with the appropriate code in that callable.

See Form fields | Django documentation | Django and Model field reference | Django documentation | Django

I also suggest you review the examples for the view in the Working with forms docs to understand the general flow for using forms.

Here is the complete views

@login_required(login_url='login')
def book_bus(request):
  if request.method == 'POST' :
    form = bookbusform(request.POST)
    if form.is_valid() :
      cd = form.cleaned_data
      current_loc = cd['current_location']
      destination = cd['Destination']
      departure = cd['departure_date']
      User = get_user_model()
      if 'booknow' in request.POST  :
        user = User.objects.get(phone_number=request.user.phone_number)
        booking = Booking.objects.create(current_Location = current_loc,destination = destination,departure_date = departure)
        booking.created_by  = user
        booking.save()
        return redirect('payonline')
      else :
        user = User.objects.get(phone_number=request.user.phone_number)
        booking = Booking.objects.create(current_Location = current_loc,destination = destination,departure_date = departure)
        booking.created_by  = user
        booking.save()
        return redirect('home')

    else :
      return redirect('error-page')

  else :
    form = bookbusform()
    context = {
      'form' : form
      }
    return render(request,'bookbus.html',context)

I don’t understand can you explain further to me I’m new to Django

First, you were using an apostrophe - ' instead of the backtick - ` characters to mark off your code. I fixed that for you this time.

Side note: The first two fields would be better off as ModelChoiceField instead of ChoiceField, since they’re related to a model.

The specific reason that you are always getting is_valid being false is because you’re not providing any options for the Destination field. Since you’re returning None from your queryset, there are no valid choices available, and whatever is entered in the form is going to fail the test.

1 Like

Thanks, for helping me

You are suggesting I use ModelChoiceField ?

How can I solve this issue, I need to set destination to an empty select field, making sure that the user does not select anything in the destination field until dey choose from the current_location field and when they choose the current_location and ajax request is made and the options in current_location is omitted and the new response (destination) is sent in a new html form and when the user click submit the whole form is submitted with the valid option, how can I do that ?

This process - where you have a select field dependent upon a prior selection - is known as a “chained”, “cascade”, or “dependent” select. There have been a few discussions here about implementing them.

Start with Values from the second field of the form - depending on the selected value of the first field? and read some of the links from there as well. Then if you have any further questions, feel free to come back here and add them.

You haven’t shown your Booking model here, so I can’t say for sure - but if your Booking model has the field defined as a Foreign Key, then yes.