Reportlab - Python - Doubt with date writing (date)

Asked

Viewed 197 times

0

Guys I’m having doubts in the reportlab.

Python 3 Application / Django

I have a model Pessoa that contains a field data de nascimento who’s kind DateTimeField. I’m trying to write the date of birth in the PDF but the error. Remembering that the date comes from the database.

Other fields like Name, city, address, etc are writing in PDF correctly.

p = Pessoa.objects.get(pk=pk)
c.drawString(200, 630, p.data_nascimento)

inserir a descrição da imagem aqui

I’m using the Reportlab (http://www.reportlab.com/)

  • About the second problem, it has more to do with the client’s browser, or something in Javascript. But it will not be done through the backend.

1 answer

2


datetime.datetime object has no attribute 'decode'

This error means that an object in the class datetime.datetime is being called, somewhere, with the attribute/method decode.

The method decode, in Python3, is used by the bytes class to convert to string.

Follow the example:

>>> s = b'Oi'
>>> type(s)
<class 'bytes'>
>>> s = s.decode()
>>> s
'Oi'
>>> type(s)
<class 'str'>

To solve your problem, I believe the best way is to transformp.data_nascimento(an object of the datetime.datetime class) for bytes or str before sending to the Drawstring function. To do this, use strftime.

p = Pessoa.objects.get(pk=pk)
c.drawString(200, 630, p.data_nascimento.strftime('%d/%m/%Y'))
  • thank you very much. That’s right. I even googled, but I didn’t know how to put this strftime .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.