amr_aly

Hakim_Zerrouki_Model_PDF

Jul 19th, 2021 (edited)
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. ######################## in views.py
  2.  
  3. from .models import Conso
  4.  
  5. def print_html(request):
  6.     qs = Conso.objects.all().order_by('-date') ## you can order by '-id' field if you want
  7.  
  8.     context = {
  9.             'qs': qs,
  10.     }
  11.     return render(request, 'print.html', context)
  12.  
  13.  
  14. ######################### in print.html
  15.  
  16. <div class="container">
  17.             <button class="btn btn-outline-success" id="print" onclick="printDiv()">
  18.             Print
  19.             </button>
  20. </div>
  21.  
  22. {% block content %}
  23. <table class="fl-table" id="table">
  24.        <thead>
  25.              <tr>
  26.                  <th> الرقم </th>
  27.                  <th> utlisateur </th>
  28.                  <th> العدد </th>
  29.                  <th> التاريخ </th>
  30.                  <th> OBS </th>
  31.              </tr>
  32.       </thead>
  33.     {% for obj in qs %}
  34.         <tbody>
  35.              <tr>
  36.                  <td>{{obj.id}} </td>
  37.                  <td>{{obj.utlisateur}} </td>
  38.                  <td>{{obj.coun_mat }} </td>
  39.                  <td>{{obj.date }} </td>
  40.                  <td>{{obj.OBS }}</td>
  41.              </tr>
  42.         </tbody>
  43.     {% endfor %}
  44.  
  45. </table>
  46. {% endblock  %}
  47.  
  48. {% block scripts %}
  49.  
  50. <script type="text/javascript">
  51.  
  52.     function printData() {
  53.     var divToPrint = document.getElementById('pdata');
  54.     var htmlToPrint = '' +
  55.         '<style type="text/css">' +
  56.         'table th, table td {' +
  57.         'border:1px solid #000;' +
  58.         'padding:0.5em;' +
  59.         'width:100%;' +
  60.         'white-space:nowrap;' + // this line to make the cells flixable
  61.         '}' +
  62.         'p {' +
  63.         'text-align: right' +
  64.         'margin-bottom:0;' +
  65.         'padding:0;' +
  66.         'line-height:12px;' +
  67.         'font-size:10px;' +
  68.         '}' +
  69.         '</style>';
  70.     htmlToPrint += divToPrint.outerHTML;
  71.     newWin = window.open("");
  72.     newWin.document.write(htmlToPrint);
  73.     newWin.print();
  74.     newWin.close();
  75. }
  76.  
  77. </script>
  78. {% endblock  %}
  79.  
  80.  
  81.  
  82. ##################### in app/urls.py
  83. from .views import print_html
  84.  
  85. app_name = 'your_app_name'
  86. urlpattern = [
  87. path('export/model/to/pdf/', print_html, name='print_html'),
  88.  
  89. ]
  90.  
  91.  
Add Comment
Please, Sign In to add comment