mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Replaced cStringIO.StringIO by io.BytesIO.
Also replaced StringIO.StringIO by BytesIO in some other appropriate places. StringIO is not available in Python 3.
This commit is contained in:
parent
1583d40224
commit
d7dfab59ea
26 changed files with 94 additions and 167 deletions
|
@ -104,15 +104,11 @@ Complex PDFs
|
|||
============
|
||||
|
||||
If you're creating a complex PDF document with ReportLab, consider using the
|
||||
:mod:`cStringIO` library as a temporary holding place for your PDF file. This
|
||||
:mod:`io` library as a temporary holding place for your PDF file. This
|
||||
library provides a file-like object interface that is particularly efficient.
|
||||
Here's the above "Hello World" example rewritten to use :mod:`cStringIO`::
|
||||
Here's the above "Hello World" example rewritten to use :mod:`io`::
|
||||
|
||||
# Fall back to StringIO in environments where cStringIO is not available
|
||||
try:
|
||||
from cStringIO import StringIO
|
||||
except ImportError:
|
||||
from StringIO import StringIO
|
||||
from io import BytesIO
|
||||
from reportlab.pdfgen import canvas
|
||||
from django.http import HttpResponse
|
||||
|
||||
|
@ -121,9 +117,9 @@ Here's the above "Hello World" example rewritten to use :mod:`cStringIO`::
|
|||
response = HttpResponse(mimetype='application/pdf')
|
||||
response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
|
||||
|
||||
buffer = StringIO()
|
||||
buffer = BytesIO()
|
||||
|
||||
# Create the PDF object, using the StringIO object as its "file."
|
||||
# Create the PDF object, using the BytesIO object as its "file."
|
||||
p = canvas.Canvas(buffer)
|
||||
|
||||
# Draw things on the PDF. Here's where the PDF generation happens.
|
||||
|
@ -134,7 +130,7 @@ Here's the above "Hello World" example rewritten to use :mod:`cStringIO`::
|
|||
p.showPage()
|
||||
p.save()
|
||||
|
||||
# Get the value of the StringIO buffer and write it to the response.
|
||||
# Get the value of the BytesIO buffer and write it to the response.
|
||||
pdf = buffer.getvalue()
|
||||
buffer.close()
|
||||
response.write(pdf)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue