VIEWS.PYreturn render(request, "home.html", {'data':data'})HOME.HTML<html>
<head>
<script src="{% static '/js/app.js' %}"></script>
<script>
myFunction({{ data | safe }});
</script>
</head>
</html>
APP.JS
function myFunction(vars){
return vars};
I would like to have the JS in the HTML file in the APP.JS file so I can centralize all Javascript, but cannot get it to work outside the HTML. I also tried sending the "data" to JSON using "json.dumps" but got an error that the data was not serializable, so went with this code posted above.
Can I do what I'm trying to do?
Thanks
Steve
class InstructorDeleteView(DeleteView):
model = Instructor
success_url = reverse_lazy('instructor_list')
template_name = 'class_management/instructor_list.html'
def delete(self, request, *args, **kwargs):
"""
Calls the delete() method on the fetched object and then
responds to the call with the relevant data.
"""
self.object = self.get_object()
pk = self.object.pk # Save the primary key before the object is deleted.
self.object.delete()
data = {
'message': "Successfully deleted the instructor.",
'pk': pk,
}
return JsonResponse(data)
$.ajax({
method: "POST",
url: $id + "/delete/",
data: $data,
success: handleSuccess,
error: handleError,
});
function handleSuccess(data, textStatus, jqXHR) {
console.log("SUCCESSFULLY DELETED INSTRUCTOR.");
console.log("Removing the deleted entry from the table.");
$('#row' + data['pk']).remove();
console.log(data);
console.log(textStatus);
console.log(jqXHR);
}
function handleError(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown)
}