handling ajax requests in django
// Posting data
$.ajax({
type: 'POST',
url: '/home/add_to_newsletters/',
// Data object along with the csrfmiddlewaretoken
data: {
'email': email.value,
'csrfmiddlewaretoken': document.getElementsByName('csrfmiddlewaretoken')[0].value,
},
success: function(resp){
// Do something on success
}
});
class AddToNewsletters(View):
def post(self, request):
posted_email = request.POST['email']
print(posted_email)
return HttpResponse("Ajax with Django Success!")