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.
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.
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.