the ideal way to do it is using ajax ,so your page will not refresh every time you click the button,what missing in your question the condition of model which you want to increase ,in my code i set it news id you can change it ,check this :
<input id='news_id' name='news_id' >
<input type='button' onclick='Increase()' name='increase' >
<srcipt>
function Increase(){
var new_id= document.getElementById("news_id").value;
$.ajax({
type:"POST",
data:JSON.stringify({'data':new_id}),
success:function(responsedata){
// process on data
}
})
}
But if you want to use ajax you have to add a csrf_exempt decorator on your view
from django.views.decorators.csrf import csrf_exempt
#then write this before your view
@csrf_exempt
you can use F() object to generate a SQL expression that describes the required operation without actually having to pull them out of the database into Python memory check here you can use this :
if request.is_ajax():
new_id = json.loads(request.body.decode('utf-8'))["data"]
_reponse = Mainnews.objects.filter(id=new_id).update(likes=F('likes') + 1)
return JsonResponse({'data_to_return':'data'})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…