I want to show a pop-up instead of the httpresponse shown as below in views.py after satisfying the if condition - (below is section of code in views.py)
if data['check_in'] > now and data['check_out'] > now and data['check_out'] > data['check_in']:
#Check if the room is available
if available_rooms is not None:
if len(available_rooms) >= int(num_rooms_req):
for i in num_rooms_req_lst:
#Book the First available room
# print(available_rooms[i-1])
booking = book_room(request, available_rooms[i-1],data['check_in'],data['check_out'],total_cost)
return HttpResponse(booking)
else:
return HttpResponse('Only 1 rooms available in this category')
else:
return HttpResponse('All the rooms of this category are booked! Try another category!')
else:
return HttpResponse('checkin/checkout date in past or checkout date is prior to checkin date!')
so in the above views.py code section - what would be the best way to show the return HttpResponse pop-ups or alerts (but alerts would take me to different html) - I don't want to navigate away from the parent html...
CodePudding user response:
from django.contrib import messages
if data['check_in'] > now and data['check_out'] > now and data['check_out'] > data['check_in']:
if the room is available
if available_rooms is not None:
if len(available_rooms) >= int(num_rooms_req):
for i in num_rooms_req_lst:
booking = book_room(request, available_rooms[i-1],data['check_in'],data['check_out'],total_cost)
return HttpResponse(booking)
else:
messages.warning(request,'Only 1 rooms available in this category')
return redirect('rediect_path') #/user/login/
else:
messages.warning(request,'All the rooms of this category are booked! Try another category!')
return redirect('rediect_path') #add your rediect path
else:
messages.success(request,'checkin/checkout date in past or checkout date is prior to checkin date!')
return redirect('rediect_path')
add this in your HTML
{% if messages %}
<ul >
{% for message in messages %}
<li >{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
We can add these 5 types of messages.
debug,info,success,warning,error.
messages.debug(request, 'Total records updated {0}'.format(count))
messages.info(request, 'Improve your profile today!')
messages.success(request, 'Your password was updated successfully!')
messages.warning(request, 'Please correct the error below.')
messages.error(request, 'An unexpected error occured.')
CodePudding user response:
@Akash Nagtilak - thanks a lot of answering I tried using the above but not sure of the couple of stuff -
- in the rediect_path what can be given because in my code snippet which I posted, its the post section of the views.py which I have added but in the get section I have the html with the context which I want to remain on - for your reference I am pasting the complete class of views.py
- where should I add the portion which you've given in HTML -
class RoomDetailView(View):
def get(self, request, *args, **kwargs):
id = self.kwargs.get('id', None)
form = AvailabilityForm()
print(form)
human_format_room_category = get_room_category_human_format(id)
if human_format_room_category is not None:
context={
'room_category': human_format_room_category,
'form': form,
}
return render(request,'room_detail_view.html', context)
else:
return HttpResponse('category does not exist')
def post(self, request, *args, **kwargs):
#Get room category from kwargs
id = self.kwargs.get('id', None)
#Pass request.POST to AvailabilityForm
form = AvailabilityForm(request.POST)
#Checking form Validity
if form.is_valid(): #check the validity of the form and return the cleaned data from the form
data = form.cleaned_data
now = timezone.now()
num_rooms_req = request.POST.get('Num_Rooms')
print("no rooms requested",num_rooms_req)
num_rooms_req_lst = list(range(1, int(num_rooms_req) 1))
#Get Available Rooms
available_rooms = get_available_rooms(id, data['check_in'],data['check_out'])
# print("rooms_available",len(available_rooms))
#get the total charge for the room
total_cost = find_total_room_charge(data['check_in'],data['check_out'],id)
if data['check_in'] > now and data['check_out'] > now and data['check_out'] > data['check_in']:
#Check if the room is available
if available_rooms is not None:
if len(available_rooms) >= int(num_rooms_req):
for i in num_rooms_req_lst:
#Book the First available room
# print(available_rooms[i-1])
booking = book_room(request, available_rooms[i-1],data['check_in'],data['check_out'],total_cost)
return HttpResponse(booking)
else:
messages.warning(request,'Only 1 rooms available in this category')
return HttpResponse('redirect_path')
else:
return HttpResponse('All the rooms of this category are booked! Try another category!')
else:
return HttpResponse('checkin/checkout date in past or checkout date is prior to checkin date!')
@Omayer Hasan Marif -- thanks for your reply
I need a pop-up from the html which I am calling in the get section of the above code snippet which I've shared i.e. - room_detail_view.html
not sure if I understand about the API...
