如何使用ajax在DB的div区域的前端显示内容?

时间:2018-08-13 10:59:18

标签: jquery ajax django postgresql

我想在#search-results区域的“ Get Reports”按钮下方单击“ Get Reports”按钮后显示内容。 然后将使用所选日期的内容过滤数据。(请参见屏幕截图)。

实际结果:我在开发人员工具中获得了预期的结果,而不是在“#search-results”部分中。

 #views.py
from django.shortcuts import render, get_object_or_404,render_to_response
from .models import Statusreport


 def statusreport(request):
    return render(request, "statusreport.html")

 def search_report(request):
   if request.method == 'POST':
     search = request.POST['search']
     print("checkoutput"+search)
   else:
     search = ''

 statusreport = Statusreport.objects.filter(created_date__contains=search)

 print(statusreport)

 return render_to_response("statusreport-detail.html", {'statusreport': 
 statusreport})

   #statusreport.html
        <main class="container">
         < form method="POST">
          {% csrf_token %}
            <div class="row" style="padding-top: 100px">
                <div class="col">
                 <label class = 'control-label' for="datepicker">Select Date: 
                  </label>
              <input data-date-format="yyyy-mm-dd" id="datepicker">
              </br>
           </br>
             <button type="button" class="btn btn-info">GET REPORTS</button>

                </div>
           </div>
        </form>
     </main>

  <script 
     src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
 </script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap- 
   datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>
 <script 
  src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" 
  integrity="sha384- 
  JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" 
  crossorigin="anonymous"></script>
  <style type="text/css">
       // solution 1:
        .datepicker {
           font-size: 0.875em;
         }


    .datepicker td, .datepicker th {
        width: 1.5em;
        height: 1.5em;
    }

</style>
  <script type="text/javascript">
      $('#datepicker').datepicker({
         weekStart: 1,
         daysOfWeekHighlighted: "6,0",
         autoclose: true,
         todayHighlight: true,
         });
      $('#datepicker').datepicker("setDate", new Date());

    $(document).ready(function(){
       $("button").click(function(){
           $.ajax({
              type : 'POST',
               url: "/statusreport/statusreport-detail/",
              data: {
            'search' : $('#datepicker').val(),
            'csrfmiddlewaretoken' : 
           $('input[name=csrfmiddlewaretoken]').val()
      },
    sucess : searchSuccess,
    dataType: 'html'

      });
    });
  });
 function searchSuccess(data, textStatus, jqXHR)
  {

   $('#search-results').html(data);
   }

</script>

<div id = 'search-results'>


</div>

#statusreport-detail.html
 <table class="table table-bordered " border="1">
 <thead>
 <tr><th>id</th>
 <th>created_date</th>
 <th>taskname</th>
 <th>testrun</th>
 <th>total</th>
 <th>passed</th>
 <th>failed</th>
 <th>blocked</th>
 <th>inprogress</th>
 <th>not_implemented</th>
 <th>not_applicable</th>
  <th>untested</th>
 </tr>
 </thead>

<tbody>

{% for obj in statusreport%}
 <tr>
<td> {{ obj.id }} </td>
<td> {{ obj.created_date }} </td>
<td>  {{ obj.taskname }}  </td>
<td>  {{ obj.testrun }} </td>
<td>  {{ obj.total }} </td>
<td>  {{ obj.passed }} </td>
<td>  {{ obj.failed }} </td>
<td>  {{ obj.blocked }} </td>
<td>  {{ obj.inprogress }} </td>
<td>  {{ obj.not_implemented }} </td>
<td>  {{ obj.not_applicable }} </td>
<td>  {{ obj.untested }} </td>
</tr>

{% endfor %}

</tbody></table>
#urls.py
from django.urls import path

app_name = "statusreport"

from .views import (
 search_report,statusreport
  )

 urlpatterns = [

path('' , statusreport, name = 'statusreport'),
path("statusreport-detail/", search_report, name = 'statusreport-detail')

 ]

enter image description here

该数据需要反映在“获取报告”下方。

1 个答案:

答案 0 :(得分:0)

尝试一下。...

<button type="button" class="btn btn-info">GET REPORTS</button>

$("#button").click(function(){
    $("#mytable").hide();

    $.ajax({
        url:'/statusreport/statusreport-detail/',
        type: "POST",
        data: "{
                    'search' : $('#datepicker').val(),
                    'csrfmiddlewaretoken' : $('input[name=csrfmiddlewaretoken]').val()
                }"
        dataType: 'json',
        success: function(data){
            $.each(data, function(key, value){
                $("table #mytable1").append("<tr><td>" +
                                    "ID :" + value.ID +
                                    "created_date :"+ value.Name +
                                    "taskname :" + value.Age + 
                                    "</td><tr>");
                                   .........
            });
        }
    });
});