How to check Django Form Submission Against Value in Database Table
Question:
I am trying to create a function in my views.py that checks a users date submission against values already submitted to the database:
def make_booking(request):
if request.method == 'POST':
form = BookingForm(request.POST)
if form.is_valid():
new_date = form.cleaned_data.get("date_of_booking")
existing_booking = Booking.objects.get('date_of_booking').items()
for booking in existing_booking:
if booking == new_date:
messages.success(request, "THIS DATE IS ALREADY TAKEN")
else:
form.save()
print('form saved')
if request.user.is_authenticated:
return redirect(get_bookings)
if not request.user.is_authenticated:
return redirect(get_bookings_guest)
If the date already exists in the database then I want it to show a message saying the date already exists. I have written the above which seems correct to me but I get this error: "Exception Value:
too many values to unpack (expected 2)".
Please could someone help me?
Answers:
How about filtering the Booking
objects in database for all entries with new_date
? Make the if
comparison based on the number of entries returned; if zero, new_date
is a new date.
Assuming that new_date
is a datetime
object, you could do this:
if form.is_valid():
new_date = form.cleaned_data.get("date_of_booking")
existing_bookings = Booking.objects.filter(date_of_booking=new_date)
if existing_bookings.count() == 0:
form.save()
print('form saved')
else:
messages.success(request, "THIS DATE IS ALREADY TAKEN")
Try this:
if form.is_valid():
new_date = form.cleaned_data.get("date_of_booking")
if Booking.objects.filter(date_of_booking=new_date).exists():
messages.success(request, "THIS DATE IS ALREADY TAKEN")
else:
form.save()
print('form saved')
Sample with time
new_date = form.cleaned_data.get("date_of_booking")
new_time = form.cleaned_data.get("time_of_booking")
if Booking.objects.filter(date_of_booking=new_date).filter(
time_of_booking=new_time).exists():
messages.success(request, "THIS DATE AND TIME IS ALREADY TAKEN")
else:
form.save()
print('form saved')
I am trying to create a function in my views.py that checks a users date submission against values already submitted to the database:
def make_booking(request):
if request.method == 'POST':
form = BookingForm(request.POST)
if form.is_valid():
new_date = form.cleaned_data.get("date_of_booking")
existing_booking = Booking.objects.get('date_of_booking').items()
for booking in existing_booking:
if booking == new_date:
messages.success(request, "THIS DATE IS ALREADY TAKEN")
else:
form.save()
print('form saved')
if request.user.is_authenticated:
return redirect(get_bookings)
if not request.user.is_authenticated:
return redirect(get_bookings_guest)
If the date already exists in the database then I want it to show a message saying the date already exists. I have written the above which seems correct to me but I get this error: "Exception Value:
too many values to unpack (expected 2)".
Please could someone help me?
How about filtering the Booking
objects in database for all entries with new_date
? Make the if
comparison based on the number of entries returned; if zero, new_date
is a new date.
Assuming that new_date
is a datetime
object, you could do this:
if form.is_valid():
new_date = form.cleaned_data.get("date_of_booking")
existing_bookings = Booking.objects.filter(date_of_booking=new_date)
if existing_bookings.count() == 0:
form.save()
print('form saved')
else:
messages.success(request, "THIS DATE IS ALREADY TAKEN")
Try this:
if form.is_valid():
new_date = form.cleaned_data.get("date_of_booking")
if Booking.objects.filter(date_of_booking=new_date).exists():
messages.success(request, "THIS DATE IS ALREADY TAKEN")
else:
form.save()
print('form saved')
Sample with time
new_date = form.cleaned_data.get("date_of_booking")
new_time = form.cleaned_data.get("time_of_booking")
if Booking.objects.filter(date_of_booking=new_date).filter(
time_of_booking=new_time).exists():
messages.success(request, "THIS DATE AND TIME IS ALREADY TAKEN")
else:
form.save()
print('form saved')