Django query data in template using JS and python functions
Let's say I have a form for a booking system based on a one-to-many relations with the visitors table..as in the attached image, I'm trying to implement a "Find" function linked to the visitor passport table so we don't need to re-enter the visitors data again. abasically we can enter the passport number and run a query so if his data is there it will come back and fil the form, otherwise we have to reenter it manually using a different form.
The following JS function will query the visitor table by passport # and bring back data correctly. However I don't know how to fill out the booking form with the new data so it can be saved, because the data I get is in html format as a table. In my real case, I need to save two fields only from the returned data including the passport number in the booking form, so how I extract the passport # from the returned data, or maybe there is a better way to do so.
function visitor_details(pass_num){
url = 'booking_vis_details'
$.ajax({
type: "GET",
url : url,
data: {
num: pass_num,
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success: function(data) {
$('#table_details').html(data);
}
})
}
View:
def booking_vis_details(request):
num = request.GET.get('num')
data = Visitor.objects.get(passport_num=num)
if not data: data = []
return render(request, 'booking_form/booking_vis_details.html', {'form': data})