I need to implement a JavaScript function in my view. The scenario is as follows: If a user clicks on the 'book' button without selecting an article, the book function should check the database and if it finds that no product has been chosen, it should display a message on the screen saying "Sorry, first choose a product."
I am considering adding this message in my view function after checking the database. Is it possible to return a JavaScript function to the page?
Pseudo code:
function book():
products = Produkt.objects.all()
if products == None:
return HttpResponse("<script type='text/javascript'> some function? </script>")
return render_to_response('book.html', {'products':products}, context_instance=RequestContext(request))
Is this the correct approach?
Thank you