Я использую CBV и мне нужно изменить значение поля при нажатии кнопки.Я попробовал некоторый код, но он не работает.Что не так в этом коде и что можно изменить?
script
$("#ajax_button").on('click', function () {
$.ajax({
url: 'NewApp/ajax/change_status/{{userprofile.id}}',
data: {
'verified': False ,
},
dataType: 'json',
beforeSend:function(){
return confirm("Are you sure?");
success: function (data) {
if (data.success) {
alert("ajax call success.");
// here you update the HTML to change the active to innactive
}else{
alert("ajax call not success.");
}
}
});
});
views.py
def ajax_change_status(request,pk):
userpr = UserProfile.objects.get(pk=pk)
try:
userpr.verified = True
userpr.save()
return JsonResponse({"success": True})
except Exception as e:
return JsonResponse({"success": False})
return JsonResponse(data)
urls.py
url(r'^ajax/change_status/(?P<pk>\d+)/$', views.ajax_change_status, name='ajax_verified')