Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

python - Django change data used in template based on checkbox dynamically

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...