when i remove the html data- , then django field value is showing there . but when i add the data-id, data-winner etc , django model field default value is not showing in template, how can i fix this . ?
models.py
class Contest(models.Model):
title = models.CharField(max_length=50)
name_winner = models.CharField(max_length=50,default="Announce")
@property
def winner(self):
if something ...
winner_name = something
else:
winner_name = something
return name_winner
html
{%for ....%}
<td type="hidden" class="winner_click" data-winner="{{contest.winner}}" data-id="{{contest.id}}"> {{contest.name_winner}}</td>
{%end for%}
<script>
$(".winner_click").click(function(){
var get_id = $(this).data('id')
var get_data = $(this).data('winner')
$(this).html(get_data).css("color","black")
$.ajax(
{
type:"POST",
url: "/admin/save_winner",
data:{
'contest_id': get_id,
'contest_winner':get_data,
"csrfmiddlewaretoken": '{{ csrf_token }}'
},
success: function( data )
{
$(this).html(get_data).css("color","black")
}
})
});
</script>
in this case when i remove the data-id and data-winner then that td shows default django field value , in this case there is nothing shows .
views: save winner
def save_winner(request):
if request.method == 'POST':
contest_id = request.POST.get('contest_id')
contest_winner = request.POST.get('contest_winner')
Contest.objects.filter(id=contest_id).update(name_winner=contest_winner)
return JsonResponse({"status":"success"})
how can i fix this ? why is this issue ?
Thank you