i want to return in my view a js function. the logic is this:
if a user clicks on a button ‘book’ without choosing an article, book function will look up in database and sees that there is no product chosen in db, then the page shouldnot go further, but it should give a js message on screen saying that “sorry, first choose a product”.
my thought is that i want to give this message in my view function after lookup in db. is it possible that i can return javascript function to page?
pseudo:
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 it correct way?
thanks
Django can return anything, but clients’s browser will not be able to use it this way. You should intercept click on the link, make an AJAX call to the server, retrieve some data (HTML, or JSON with books list, or something else) and process it in javascript (maybe using jQuery or some other js framework).