I have a database table that contains sets
which each have a unique code. These codes are either 3 or 4 characters long. Sets
with codes that are 4 characters long represent extra data that is not really needed and by default I filter out these sets before sending the data to the template.
However I would like it so the the user can click a checkbox and display those extra sets
in the table on the template.
To do this I just an Ajax request to the view to send whether the checkbox is checked or not, and then change the base set
data. In the view I use an If statement to decides what data is returned.
I have checked and the If statement is working, however the data in the template is not changing.
js:
function ShowExtraSets(e) {
console.log(e.checked)
$.ajax({
url: '',
type: 'POST',
data: {
csrfmiddlewaretoken : csrf_token,
showExtraSets : e.checked
}
});
}
view:
def sets_page(request):
if 'showExtraSets' not in request.POST:
set_list = Set.objects.extra(where=["CHAR_LENGTH(code) < 4"]).order_by('-releaseDate', 'name')
elif request.POST['showExtraSets'] == 'true':
set_list = Set.objects.order_by('-releaseDate', 'name')
else:
set_list = Set.objects.extra(where=["CHAR_LENGTH(code) < 4"]).order_by('-releaseDate', 'name')
set_filter = SetFilter(request.GET, queryset=set_list)
set_list = set_filter.qs
...
question from:
https://stackoverflow.com/questions/65916479/django-change-data-used-in-template-based-on-checkbox-dynamically 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…