如何在Django通用视图中生成PDF?

时间:2019-01-30 11:04:46

标签: django django-models django-forms django-templates django-views

我有一个由通用列表视图呈现的模板。我想在表格中每行数据的前面提供一个下载链接。下载链接将创建相应行数据的PDF文件。请告诉我如何为此编写代码?

Views.py

class BookingConfirmationListView(LoginRequiredMixin, generic.ListView):
    model = container_booking
    template_name = 'home/booking_confirmation_detail.html'
    context_object_name = 'all_container'

    def get_queryset(self):
        return container_booking.objects.all()

模板看起来像

<table class="table-striped table-hover table-bordered" width="100%">
  <tr>
    <th>Date</th>
    <th>Source</th>
    <th>Destination</th>
    <th>Container</th>
    <th>Commodity</th>
    <th>Agreed Rate</th>
    <th>Edit</th>
    <th>Delete</th>
    <th>Status</th>
  </tr>
{% for item in all_container %}
  <tr>
    <td>{{ item.date }}&nbsp;</td>
    <td>{{ item.place_of_reciept }}&nbsp;</td>
    <td>{{ item.final_place_of_destination }}&nbsp;</td>
    <td>{{ item.equipment_type }}{{ item.quantity }}&nbsp;</td>
    <td>{{ item.commodity }}&nbsp;</td>
    <td>{{ item.agreed_rate }}&nbsp;</td>
    <td><a href="{% url 'home:cont_bk-update' item.id %}" ><i class="fas 
         fa-edit"></i></a></td>
    <td><a href="{% url 'home:cont_bk-delete' item.id %}" ><i class="fas 
         fa-trash"></i></a></td>
    <td>{{ item.approved }}</td>
  </tr>
{% endfor %}
</table>

urls.py

url(r'^cont_bkdetail$', views.BookingConfirmationListView.as_view(), 
name='cont_bk-detail'),
url(r'^cont_bk/(?P<pk>[0-9]+)/$', 
views.BookingConfirmationUpdate.as_view(), name='cont_bk-update'),
url(r'^cont_bk/(?P<pk>[0-9]+)/delete/$', 
views.BookingConfirmationDelete.as_view(), name='cont_bk-delete'),

我希望每当我单击下载时,都会生成该行的PDF文件。

0 个答案:

没有答案