我想从表中选择一条记录,以便在Django中转到另一个页面

mrfwxfqh  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(89)

我有一个表在我的模板有很多记录,从数据库中检索,每行有一个选择选项,当用户选择一个选项,并点击按钮,它必须带他到另一个页面.我的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>

字符串

url.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;
      });
    });


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

wpx232ag

wpx232ag1#

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

<div><a href="{% url 'your_url' %}" button id="go-button">Your Button Text</button></a></div>

字符串

相关问题