我想从表中选择一条记录以转到django中的另一个页面

问题描述 投票:0回答:1

我的模板中有一个表,其中包含从数据库检索的许多记录,每一行都有一个选择选项,当用户选择一个选项并单击按钮时,必须将他带到另一个页面。 我的template.html

<table id="tblToExcl" class="table table-striped table-bordered table-sm">  
      <thead class="thead-dark">  
      <tr>  
       
        <th>رقم الكتاب</th>  
          <th>تاريخ</th>  
          <th>تاريخ الاضافة</th>
          <th>ملاحظات</th>  
          <th>اﻻجراء</th> 
          
      </tr>  
      </thead>  
      <tbody>  
  {% for doc in docs %}  
      <tr>  
         
          <td>{{ doc.number }}</td>  
          <td>{{ doc.date|date:"Y-m-d"}}</td> 
          <td>{{doc.created_at|date:"Y-m-d"}}</td> 
          <td>{{ doc.note }}</td> 
          <td>{{ doc.status }}</td>
          
        
          <td>  
              <a href="/edit/{{ doc.id }}"><span class="glyphicon glyphicon-pencil" ><i class="far fa-edit" style="color: #116014;"></i></i></span></a>  
              <a href="/delete/{{ doc.id }}"><i class="far fa-trash-alt" style="color: #bf5a40;"></i></a>  
              <a href="/download/{{ doc.id }}"><i class="far fa-file-pdf" style="color: #204a87;"></i></a>
          </td>  

          <td>
            <select id="choices" style="background-color:DodgerBlue; color: white;">
              <option value="/deliver/{{ doc.id }}/{{doc.number}}/{{doc.date}}/{{doc.note}}/{{doc.document}}/{{doc.status}}">الموارد البشرية</option>
              <option value="">المالية</option>
              
            </select>
            
            <button type="button" id="go-button" class="button"><span>تحويل</span></button>
           

          </td>
      </tr>  
  {% endfor %}  
      </tbody>  
  
  </table> 

urls.py:

urlpatterns = [
    path("", views.index, name="index"),
    path("show", views.show, name="show"),
    path("docs", views.documents, name="docs"),
    path("maward-show", views.show_maward, name="maward-show"),
    path("deliver/<int:id>/<int:number>/<str:date>/<str:note>/<path:document>/<str:status>", views.deliver, name="deliver"),]

**views.py: **

def deliver(request, id, number, date, note, document, status):
    print(document)
    var = Maward.objects.create(id=id, number=number, date=date, note=note, document=document, status=status)
    time.sleep(3)
    return redirect("/show") 

我在我的模板中添加了一段 JavaScript 代码,如下所示:

<script>
    $(document).ready(function() {
      $('#go-button').click(function() {
        // Get the selected choice
        var selectedChoice = $('#choices').val();
    
        // Go to the selected page
        window.location.href = selectedChoice;
      });
    });

它仅在第一行工作正常,表的其余部分则不然,我不知道问题是什么

javascript django django-forms django-templates
1个回答
0
投票

将 href 添加到按钮中,您就可以加入您的目标

        <div><a href="{% url 'your_url' %}" button id="go-button">Your Button Text</button></a></div>
© www.soinside.com 2019 - 2024. All rights reserved.