I am new to Django. So here, I am trying to pass a two query sets from Django html template to Django views.py. This has to happen on the click of a button and hence, I am using onClick function to pass the parameters to Ajax and from there, I am sending it to views. Here, along with the two query sets, I am passing two other values called rid and category and it is working fine, but the query sets are getting converted to string and hence I am not able to iterate through the query sets after passing it to the views.py page.
This is my views.py code:
if request.is_ajax():
rid = request.POST.get('rid')
category = request.POST.get('category')
extracted_records=request.POST.get('extracted_records')
show_cards_records=request.POST.get('show_cards_records')
return render(request,'cards_update.html',{'extracted_records':extracted_records,'show_cards_records':show_cards_records,'rid':rid})
return render(request,'cardpage.html',{'extracted_records':extracted_records,'show_cards_records':show_cards_records,'rid':'1234'})
This is my main html code:
<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>CardGame</title>
<link rel="stylesheet" href="/static/css/cardpage.css"}>
</head>
<body>
<center>
<h1>{{rid}}</h1>
<div id='update_cards'>
{% include 'cards_update.html' %}
</div>
</center>
</body>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" type = "text/javascript" charset = "utf-8"></script>
<script>
function func(rid,category,extracted_records,show_cards_records) {
$.ajax({
type: 'POST',
url: '/Cardpage',
data:{
'rid':rid,
'category':category,
'extracted_records':extracted_records,
'show_cards_records':show_cards_records,
},
success: function (response) {
$("#update_cards").replaceWith(response);
alert("card casted.");
}
});
}
</script>
</html>
This is my page html code:
<center>
<button onclick="func('{{entry.rid}}','Rewards_And_Stipends','{{extracted_records}}','{{show_cards_records}}')" type="button" class="submit-btn">REWARDS : {{entry.Rewards_And_Stipends}}</button>
<button onclick="func('{{entry.rid}}','Questions_Posted','{{extracted_records}}','{{show_cards_records}}')" type="button" class="submit-btn">QUESTIONS POSTED : {{entry.Questions_Posted}}</button>
<button onclick="func('{{entry.rid}}','Questions_Answered','{{extracted_records}}','{{show_cards_records}}')" type="button" class="submit-btn">QUESTIONS ANSWERED : {{entry.Questions_Answered}}</button>
<button onclick="func('{{entry.rid}}','Quizzes_Played','{{extracted_records}}','{{show_cards_records}}')" type="button" class="submit-btn">QUIZZES PLAYED : {{entry.Quizzes_Played}}</button>
<button onclick="func('{{entry.rid}}','Mentorship','{{extracted_records}}','{{show_cards_records}}')" type="button" class="submit-btn">MENTORSHIP : {{entry.Mentorship}}</button>
</center>