Hello, am using a reportlab to generate PDF report for the user after submitting his information. But this error occurs. I dont know what am doing wrong please i need help to move on.
views
def general_pdf(request, id):
#generate bytestream buffer
buf = io.BytesIO()
#create a canvas
c =canvas.Canvas(buf, pagesize=letter, bottomup=0)
# create a text object
textobj = c.beginText()
textobj.setTextOrigin(inch, inch)
textobj.setFont('Helvetica', 14)
# design the model
persons =Personalinfo.objects.get(user_id =id)
# create blank list
lines =[]
for person in persons:
lines.append(person.first_name)
lines.append(person.second_name)
lines.append(person.gender)
lines.append(person.marital_status)
lines.append(person.date_of_birth)
lines.append(person.job)
lines.append(person.course_of_study)
lines.append(person.contact_address)
lines.append(person.passport)
lines.append(person.phone)
lines.append(person.state)
lines.append(person.lga)
#loop
for line in lines:
textobj.textLine(line)
#finish up
c.drawText(textobj)
c.showPage()
c.save()
buf.seek(0)
return FileResponse(buf, as_attachment=True, filename='CSC.pdf')
the models
class Personalinfo(models.Model):
user=models.ForeignKey(User, null=True, on_delete=models.CASCADE)
first_name =models.CharField(max_length=100, null=True)
second_name =models.CharField(max_length=100, null=True)
gender=models.CharField(choices=CHOICE_FIELD, max_length=10, null=True)
marital_status = models.CharField(choices=MARITAL_CHOICES, max_length=100, null=True)
date_of_birth =models.DateField(null =True)
job = models.ForeignKey(Job, on_delete=models.CASCADE, null=True, related_name='persoalinfo')
course_of_study=models.CharField(max_length=100, null=True)
contact_address =models.CharField(max_length=300, null=True)
passport = models.ImageField(upload_to='', null=True)
phone= models.CharField(max_length=15, null=True)
state =models.CharField(choices=STATE_CHOICES, max_length=10, null=True)
lga =models.CharField(max_length=10, null=True)
the Traceback
System check identified no issues (0 silenced).
November 18, 2022 - 12:27:58
Django version 4.0, using settings 'adamawa_civil_service_recruitment.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /general_pdf/2/
Traceback (most recent call last):
File "C:\Users\Christopher\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Christopher\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Christopher\adamawa_civil_service_recruitment\portal\views.py", line 125, in general_pdf
for person in persons:
TypeError: 'Personalinfo' object is not iterable
[18/Nov/2022 12:29:39] "GET /general_pdf/2/ HTTP/1.1" 500 63497