0

I want to print table row information in a Javascript function bind to the onclick event of a button.

view.py

def table(request):

  test_list = TestInformation.objects.order_by()
  context = { 'test_list' : test_list, }

  return render(request, '/test.html', context)

test.html

<label ~~ id="btn_click" onclick="btn()">

<table class="table table-bordered" id="data_table">
<thead>
    <tr>
        <th>ID</th>
        <th>Name</th>
    </tr>
    </thead>
    <tbody>
       {% for li in test_list %}
       <tr>
           <td> {{ li.Id }} </td>
           <td> {{ li.Name}} </td>
       </tr>
       {% endfor %}
    </tbody>
</table>

<Script type="text/javascript"> 
    function btn { 
        //I'd like to put above {for~~ endfor} source here!
    } 
</Script>

Currently Action The table is displayed as soon as you load the html page.

Expected Action Only visible when the table presses a button

How can I invoke Python objects in JavaScript functions for the actions I expect?

  • 1
    Where is your problem? i don't undesrtand – Sfili_81 Jun 04 '20 at 07:16
  • What is the specific issue you are facing? I did notice in the view code an absolute path to the template file `return render(request, '/test.html', context)`, perhaps look into where to put the templates, https://stackoverflow.com/questions/1740436/what-is-the-best-location-to-put-templates-in-django-project – martyn Jun 04 '20 at 08:37
  • @Sfili_81 The question has been modified. Ultimately, what I want to do is how to implement the function in the Java script. – Seong Gyu Choi Jun 04 '20 at 23:17

0 Answers0