Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######################## in views.py
- from .models import Conso
- def print_html(request):
- qs = Conso.objects.all().order_by('-date') ## you can order by '-id' field if you want
- context = {
- 'qs': qs,
- }
- return render(request, 'print.html', context)
- ######################### in print.html
- <div class="container">
- <button class="btn btn-outline-success" id="print" onclick="printDiv()">
- Print
- </button>
- </div>
- {% block content %}
- <table class="fl-table" id="table">
- <thead>
- <tr>
- <th> الرقم </th>
- <th> utlisateur </th>
- <th> العدد </th>
- <th> التاريخ </th>
- <th> OBS </th>
- </tr>
- </thead>
- {% for obj in qs %}
- <tbody>
- <tr>
- <td>{{obj.id}} </td>
- <td>{{obj.utlisateur}} </td>
- <td>{{obj.coun_mat }} </td>
- <td>{{obj.date }} </td>
- <td>{{obj.OBS }}</td>
- </tr>
- </tbody>
- {% endfor %}
- </table>
- {% endblock %}
- {% block scripts %}
- <script type="text/javascript">
- function printData() {
- var divToPrint = document.getElementById('pdata');
- var htmlToPrint = '' +
- '<style type="text/css">' +
- 'table th, table td {' +
- 'border:1px solid #000;' +
- 'padding:0.5em;' +
- 'width:100%;' +
- 'white-space:nowrap;' + // this line to make the cells flixable
- '}' +
- 'p {' +
- 'text-align: right' +
- 'margin-bottom:0;' +
- 'padding:0;' +
- 'line-height:12px;' +
- 'font-size:10px;' +
- '}' +
- '</style>';
- htmlToPrint += divToPrint.outerHTML;
- newWin = window.open("");
- newWin.document.write(htmlToPrint);
- newWin.print();
- newWin.close();
- }
- </script>
- {% endblock %}
- ##################### in app/urls.py
- from .views import print_html
- app_name = 'your_app_name'
- urlpattern = [
- path('export/model/to/pdf/', print_html, name='print_html'),
- ]
Add Comment
Please, Sign In to add comment