Reportlab из БД в pdf Django
инфы в инете мало. Как можно вывести данные таблицы из БД в ПДФ, проект Django ? views.py
from django.shortcuts import render
from .models import Plan
from .models import Artiles
from .forms import ArtilesForm
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
from reportlab.lib import colors
#
from django.http import HttpResponse
from django.views.generic import DetailView, UpdateView, DeleteView
def get(request):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="Damage Report File.pdf"'
width, height = A4
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER
def coord(x, y, unit=1):
x, y = x * unit, height - y * unit
return x, y
buffer = io.BytesIO()
p = canvas.Canvas(buffer, pagesize=A4)
damage_data = Artiles.objects.all()
table = Table(damage_data, colWidths=270, rowHeights=79)
table.setStyle(TableStyle([
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
]))
table.wrapOn(p, width, height)
table.wrapOn(p, width, height)
table.drawOn(p, *coord(10, 600))
p.showPage()
p.save()
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response
просто массив созданный вручную, норм выводит в пдф, из бд не хочет куда копать ? models.py
from django.db import models
class Artiles(models.Model):
title = models.CharField('Название', max_length=50)
anons = models.CharField('Анонс', max_length=250)
full_text = models.TextField('Статья')
img = models.ImageField('Картинка', upload_to='news/')
def __str__(self):
return self.title
def get_absolute_url(self):
return f'/news/{self.id}'