Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
How to send javascript list from template to request.POST (django framework)

I have the javascript below that gets all checked box and it works well.

<script>
                function addlist() {
                    var array = []
                    var checkboxes = document.querySelectorAll('input[type=checkbox]:checked')

                    for (var i = 0; i < checkboxes.length; i++) {
                        array.push(checkboxes[i].value);
                    }
                }
</script>

I want to know how to submit the list array to views.py and get it via request.POST['']

Any suggestions?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I solved the question in the follow way:

I created a hidden input to receive the list by javascript:

    <script>
        function addlist() {
            var array = []
            var checkboxes = document.querySelectorAll('input[type=checkbox]:checked')

            for (var i = 0; i < checkboxes.length; i++) {
                array.push(checkboxes[i].value);
            }

            document.getElementById('precedent_list').value = array;
        }
    </script>


<input  type="hidden" style="outline-style: none;" id="precedent_list" name="precedent_list">

And I call the javascript function when the submit button is clicked:

    <p class="w3-center" style="padding-top: 25px; padding-bottom: 25px">
          <button class="w3-round-xxlarge general_button" type="submit" onclick="addlist()">Salvar</button>
   </p>

In views.py I receive the list in the follow way:

my_list = ''
my_list_= []
if 'precedent_list' in request.POST:
    my_list = request.POST['precedent_list']
my_list = my_list.replace(',',' ')
for j in my_list.split():
    my_list_.append(int(j))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!