1👍
You need to pass Jsonresponse()
response instead of render()
response like this
views.py
from django.http import JsonResponse
def CreditSaveView(request):
if request.method == 'POST':
creditform = CreditForm(request.POST)
if creditform.is_valid():
amt = request.POST['amt']
tag = request.POST['tag']
crs = request.POST['csrfmiddlewaretoken']
CreditModel(amt=amt,tag=tag).save()
return JsonResponse({'status':'done'}
script
<script>
document.getElementById("creditbtn").addEventListener("click", function () {
let id_amt = document.getElementById('id_amt').value;
let id_tag = document.getElementById('id_tag').value;
let crs = document.getElementsByName('csrfmiddlewaretoken')[0].value
my_data = { csrfmiddlewaretoken: crs, amt: id_amt, tag: id_tag };
console.log(my_data)
$.ajax({
url: "/creditsave/",
method: "POST",
data: my_data,
// dataType: "json",
success: function (data) {
if (data.status == 'done') {
// document.getElementById('creditform').reset()
console.log(data)
document.getElementsByTagName('form')[1].reset()
}
}
});
});
</script>
Source:stackexchange.com