If I have a button on a template which sends requests via Ajax, what is the gold-standard for updating the template? For example, if I have this Ajax function which is triggered on clicking a button....
$(document).ready(
function() {
$(".js-up").click(function(){
//pass the username from index to the view, return the username from view, (silly - but ok for testing to see how it gets passed back and forth) then update the html? How?
$.ajax({
method: "POST",
url: addItem,
data:{'UserName' : user, 'csrfmiddlewaretoken': csrftoken},
success: function(data){
//labels = data.labels;
//defaultData = data.default;
console.log(data.UserName)
},
error: function(error_data){
console.log("error");
console.log(error_data)
},
});
})
$(".js-down").click(function(){
console.log('down')
//send information to database
})
}
);
which is routed to this view :
class AddItem(View):
def post(self, request, *args, **kwargs):
#print('called view')
username = request.POST.get('UserName')
print(username)
data = {
'itemAdded': 'item was added', 'UserName' : username
}
return JsonResponse(data)
supposing my view did something to the back-end model, and I wanted to update the front-end. What are the best ways to do it? I see people who basically pass HTML via the response of the Ajax function.
Is that the best, or only way to do it - if not what are better, cleaner ways.
I apologise for the code presented -- I'm a noob